cucumber⾃动化java_cucumber⾃动化测试官⽅教程
编辑推荐:
本⽂讲解了如何安装Cucumber,如何遵循BDD流程来开发⼀个简单的⽅法,以及如何使⽤该⽅法来评估多个场景,希望能对您有所帮助。
本⽂来⾃于博客园,由⽕龙果软件琪琪编辑、推荐。
安装
cucumber是⼀款测试⼯具。可⽤于⼤多数主流编程语⾔。⽐如JAVA、JS、Ruby、C++、Lua、Android、Kotlin、C#/F#、PHP、Python、Go、Groovy、Scala等等。其中JAVA、JS、Ruby的代码托管在cucumber下。官⽅建议选择与⽣产代码相同的平台或编程语⾔的实现。本⽂主要是JAVA平台下的介绍教程。使⽤⽅法⾮常简单,创建⼀个mvn⼯程,在l⽂件引⼊以下依赖即可。
io.cucumber
cucumber-java8
4.2.0
test
也可以根据⾻架创建cucumber项⽬。
创建⼀个空的Cucumber项⽬
python转java代码我们⾸先使⽤cucumber- prototype Maven插件创建⼀个新项⽬⽬录。打开终端,转到要创建项⽬的⽬录(⽐如本⽂是hellocucumber),运⾏以下命令:
mvn archetype:generate
\
-DarchetypeGroupId=io.cucumber \
-DarchetypeArtifactId=cucumber-archetype \
-DarchetypeVersion=2.3.1.2 \
-DgroupId=hellocucumber \
-DartifactId=hellocucumber \
-Dpackage=hellocucumber \
-Dversion=1.0.0-SNAPSHOT \
-DinteractiveMode=false
你应该得到如下结果:
[INFO] Project
created from Archetype in dir:
hellocucumber/cucumber
[INFO] -------------------------------------
[INFO] BUILD SUCCESS
[INFO] -------------------------------------
在IntelliJ IDEA(或者eclipse都⾏)中打开项⽬:
⽂件->打开…->(选择l)
选择Open as Project
现在,您已经安装了⼀个简单的Cucumber项⽬。
验证cucumber安装
mvn test
您应该看到如下内容:
------------------------------------
T E S T S
------------------------------------
Running hellocucumber.RunCucumberTest
No features found at [classpath:hellocucumber]
0 Scenarios
0 Steps
0m0.004s
Tests run: 0, Failures: 0, Errors: 0, Skipped:
0,
Time elapsed: 0.541 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped:
[INFO] -----------------------------------------
[INFO] BUILD SUCCESS
[INFO] -----------------------------------------
Cucumber的输出告诉我们它没有到任何可以运⾏的东西。
写⼀个Scenario(场景)
当我们使⽤Cucumber进⾏⾏为驱动开发时,我们使⽤具体的例⼦来指定我们希望软件做什么。 Scenario是在⽣产代码之前编写的。它们以可执⾏规范的形式开始⽣命。随着⽣产代码的出现,场景扮演了事实⽂档和⾃动化测试的⾓⾊。
在Cucumber中,⼀个example称为Scenario。Scenario定义在.feature⽂件中,这些⽂件存储在src/test/resources/hellocucumber ⽬录(或⼦⽬录)中。
⼀个具体的例⼦就是:星期天不是星期五。
创建⼀个名为src/test/resources/
hellocucumber/is_it_friday_yet.feature的⽂件,
it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday
Given today is Sunday
When I ask whether it's Friday yet
Then I should be told "Nope"
这个⽂件的第⼀⾏以关键字'''Feature'''开始:后⾯跟着⼀个名称。最好使⽤与⽂件名类似的名称。
第⼆⾏是对该特性的简要描述。Cucumber并不执⾏这⼀⾏,它只是⼀个⽂档。
第4⾏,场景:Sunday is not Friday是⼀个scenario,它是说明软件应该如何⼯作的具体⽰例。
最后三⾏以Given开头,When和Then是我们的场景的步骤。这就是Cucumber将要执⾏的操作。
看⼀个未定义的scenario报告
现在我们有了⼀个场景,我们可以让Cucumber执⾏它:
mvn test
Cucumber告诉我们有⼀个undefined的场景和三个undefined的步骤。它还建议我们使⽤⼀些代码⽚段来define这些步骤: -------------------------------------
T E S T S
-------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/
is_it_friday_yet.feature:4
Given today is Sunday # null
When I ask whether it's Friday yet # null
Then I should be told "Nope" # null
1 Scenarios (1 undefined)
3 Steps (3 undefined)
0m0.040s
You can implement missing steps with the snippets
below:
@Given("^today is Sunday$")
public void today_is_Sunday() {
@When("^I ask whether it's Friday yet$")
public void i_ask_whether_it_s_Friday_yet()
{
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String arg1) {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
复制以上未定义步骤的三个代码⽚段:
@Given("^today
is Sunday$")
public void today_is_Sunday() {
/
/ Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^I ask whether it's Friday yet$")
public void i_ask_whether_it_s_Friday_yet()
{
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String arg1) {
并将它们粘贴到src/test/java/hellocucumber/steps.java中。
看⼀个pending的scenario报告
再次运⾏Cucumber: mvn test。这次的输出略有不同:
------------------------------------
T E S T S
------------------------------------
Running hellocucumber.RunCucumberTest
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday # hellocucumber/
is_it_friday_yet.feature:4
Given today is Sunday # day_is_Sunday()
cucumber.api.PendingException: TODO: implement
me
at day_is_Sunday
(Stepdefs.java:12)
at ?.today is Sunday(hellocucumber/
is_it_friday_yet.feature:5)
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet() Then I should be told "Nope" #
Stepdefs.i_should_be_told(String)
1 Scenarios (1 pending)
3 Steps (2 skipped, 1 pending)
0m0.188s
cucumber.api.PendingException:
TODO: implement
me
at day_is_Sunday
(Stepdefs.java:12)
at ?.today is Sunday(hellocucumber/

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。