本文是作者学习笔记,每一步均有截图,想要学习Cucumber自动化测试的同学可以试着一步一步来操作。
一、创建Project——HelloCucumber
在终端中输入以下命令:
mvn archetype:generate \
"-DarchetypeGroupId=io.cucumber" \
"-DarchetypeArtifactId=cucumber-archetype" \
"-DarchetypeVersion=5.6.0" \
"-DgroupId=hellocucumber" \
"-DartifactId=hellocucumber" \
"-Dpackage=hellocucumber" \
"-Dversion=1.0.0-SNAPSHOT" \
"-DinteractiveMode=false"
也可使用https://www.jianshu.com/p/60122d38a08a中所述:
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
然后系统开始自动生成一个包名为hellocucumber的project
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
等一段时间后,可以看到以下输出:
[INFO] Archetype repository not defined. Using the one from [io.cucumber:cucumber-archetype:6.0.0-RC2] found in catalog remote
Downloading from central: https://repo.maven.apache.org/maven2/io/cucumber/cucumber-archetype/2.3.1.2/cucumber-archetype-2.3.1.2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/io/cucumber/cucumber-archetype/2.3.1.2/cucumber-archetype-2.3.1.2.pom (13 kB at 18 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/io/cucumber/cucumber-archetype/2.3.1.2/cucumber-archetype-2.3.1.2.jar
Downloaded from central: https://repo.maven.apache.org/maven2/io/cucumber/cucumber-archetype/2.3.1.2/cucumber-archetype-2.3.1.2.jar (2.0 kB at 2.7 kB/s)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: cucumber-archetype:2.3.1.2
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: hellocucumber
[INFO] Parameter: artifactId, Value: hellocucumber
[INFO] Parameter: version, Value: 1.0.0-SNAPSHOT
[INFO] Parameter: package, Value: hellocucumber
[INFO] Parameter: packageInPathFormat, Value: hellocucumber
[INFO] Parameter: package, Value: hellocucumber
[INFO] Parameter: version, Value: 1.0.0-SNAPSHOT
[INFO] Parameter: groupId, Value: hellocucumber
[INFO] Parameter: artifactId, Value: hellocucumber
[INFO] Project created from Archetype in dir: /Users/feng.li1/hellocucumber
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:41 min
[INFO] Finished at: 2020-05-19T12:03:01+08:00
[INFO] ------------------------------------------------------------------------
此刻,project就创建好了,我们可以ls看一下:
二、使用IDEA打开Project并运行
然后使用IDE打开我们建好的这个project,我使用的是IDEA
在下面terminal中使用命令:
mvn test
可以看到开始下载依赖,然后执行测试,最后出现BUILD SUCCESS
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hellocucumber.RunCucumberTest
No features found at [classpath:hellocucumber]
0 Scenarios
0 Steps
0m0.001s
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.163 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.090 s
[INFO] Finished at: 2020-05-19T12:19:50+08:00
[INFO] ------------------------------------------------------------------------
查看project目录可以知道,当前这个project没有任何代码或测试,所以执行测试的结果是
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
三、添加一个Scenario
首先我们需要添加一个.feature文件,即一个测试场景描述。
在src/test/resources/hellocucumber上右键单击,选择newfile:
然后输入is_it_friday_yet.feature,新建文件,在文件中输入以下内容:
Feature: Is 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后面的是这个feature的名称,下面第二行是解释说明。Scenario后面是这个任务场景的名称,下面使用Given-When-Then这种BDD的方式,说明执行这个任务的前置条件和结果断言,Cucumber将执行这三段操作。
目前我们添加了scenario,还需要为这个scenario添加执行代码。
四:添加测试代码
然后我们现在加入测试代码
在test/java/helloCucumber目录下的Stepdefs文件中加入如下代码:
class IsItFriday {
static String isItFriday(String today) {
return null;
}
}
public class Stepdefs {
public String today;
public String actualAnswer;
@Given("^today is Sunday$")
public void today_is_Sunday() throws Exception {
today="Sunday";
}
@When("^I ask whether it's Friday yet$")
public void i_ask_whether_it_s_Friday_yet() throws Exception {
actualAnswer = IsItFriday.isItFriday(today);
}
@Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String expectedAnswer) throws Exception {
assertEquals(expectedAnswer, actualAnswer);
}
}
以上代码是将today设置为Sunday,然后执行询问today是Friday,前文在feature中我们设置的正确答案是Nope,但此刻我们将答案设置成了null
所以此刻我们run一遍测试,得到的结果应该是不通过。我们再跑一遍mvn test
结果如下:
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 # Stepdefs.today_is_Sunday()
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)
java.lang.AssertionError: expected:<Nope> but was:<null>
Results :
Failed tests: Sunday isn't Friday(Is it Friday yet?): expected:<Nope> but was:<null>
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
可以看到,运行结果很清晰地提示我们,我们想要的结果是Nope,但是实际结果是null,所以测试不通过。
然后我们将Stepdefs文件中的IsItFriday这个class中,return null改为return “Nope”
class IsItFriday {
static String isItFriday(String today) {
return "Nope";
}
}
然后重新运行mvn test,可以看到,此刻结果为通过,
1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.081s
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.26 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.557 s
[INFO] Finished at: 2020-05-19T14:47:50+08:00
[INFO] ------------------------------------------------------------------------
至此,我们的第一个scenario成功创建并运行。
五、添加scenario
在上面的feature文件中,再添加一个scenario
Scenario: Friday is Friday
Given today is Friday
When I ask whether it's Friday yet
Then I should be told "TGIF"
然后在Stepdefs中再添加一个Given:
@Given("^today is Friday$")
public void today_is_Friday() {
this.today = "Friday";
}
此时运行mvn test,得到的结果是:
Failed scenarios:
hellocucumber/is_it_friday_yet.feature:9 # Friday is Friday
2 Scenarios (1 failed, 1 passed)
6 Steps (1 failed, 5 passed)
0m0.152s
Results :
Failed tests: Friday is Friday(Is it Friday yet?): expected:<[TGIF]> but was:<[Nope]>
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0
可以看到Cucumber一共run了2个scenario,有一个失败了;其中一共run了6个步骤,有一个失败了,失败的是第二个scenario的最后一个步骤:Friday is Friday,期望值是TGIF,但是实际值是Nope。
我们将IsItFriday这个类改一下,改为:
class IsItFriday {
static String isItFriday(String today) {
if (today.equals("Friday")) {
return "TGIF";
}
return "Nope";
}
}
然后再run一遍mvn test,可以看到此时结果为SUCCESS
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
六、添加多个测试数据
用过其他自动化测试的同学应该知道,我们可以使用变量定义多个不同的测试数据,然后同一个scenario会使用每一个测试数据来跑一遍测试。
首先,我们将feature文件里的scenario改为scenario outline,然后改一些相应的描述:
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario Outline: Today is or is not Friday
Given today is "<day>"
When I ask whether it's Friday yet
Then I should be told "<answer>"
Examples:
| day | answer |
| Friday | TGIF |
| Sunday | Nope |
| anything else! | Nope |
然后我们将Stepdefs这个类中Given-When-Then改一下,改为:
@Given("^today is \"([^\"]*)\"$")
public void today_is_Someday(String today) throws Exception {
this.today=today;
}
@When("^I ask whether it's Friday yet$")
public void i_ask_whether_it_s_Friday_yet() throws Exception {
actualAnswer = IsItFriday.isItFriday(today);
}
@Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String expectedAnswer) throws Exception {
assertEquals(expectedAnswer, actualAnswer);
}
然后运行mvn test,我们可以看到,一共执行了3次scenario,每一次用的数据对应feature中example的每一条数据,并且都通过了:
Examples:
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:11
Given today is "Friday" # Stepdefs.today_is_Someday(String)
When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()
Then I should be told "TGIF" # Stepdefs.i_should_be_told(String)
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:12
Given today is "Sunday" # Stepdefs.today_is_Someday(String)
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)
Scenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:13
Given today is "anything else!" # Stepdefs.today_is_Someday(String)
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)
3 Scenarios (3 passed)
9 Steps (9 passed)
0m0.080s
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.26 sec
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.456 s
[INFO] Finished at: 2020-05-19T16:45:18+08:00
[INFO] ------------------------------------------------------------------------
注:本文内容参考官方原版英文教程:https://cucumber.io/docs/guides/10-minute-tutorial/#create-an-empty-cucumber-project