目录
Cucumber的数据驱动
使用Examples关键字
Cucumber的数据表格
数据表格中使用Maps
Cucumber的数据驱动
首先,我们看下不使用数据驱动的例子
Login.feature
Feature: Login Action
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters username and password
Then Message displayed Login Successfully
Scenario: Successful LogOut
When User LogOut from the Application
Then Message displayed LogOut Successfully
LoginSteps.java
package steps;
import io.cucumber.java8.En;
public class LoginSteps implements En{
public LoginSteps() {
Given("^User is on Home Page$", () -> {
System.out.println("User is on Home Page");
});
When("^User Navigate to LogIn Page$", () -> {
System.out.println("User Navigate to LogIn Page");
});
Then("^Message displayed Login Successfully$", () -> {
System.out.println("Message displayed Login Successfully");
});
When("^User LogOut from the Application$", () -> {
System.out.println("User LogOut from the Application");
});
Then("^Message displayed LogOut Successfully$", () -> {
System.out.println("Message displayed LogOut Successfully");
});
And("^User enters username and password$", () -> {
String username = "test";
String password = "123456";
System.out.println("username: " + username);
System.out.println("password: " + password);
});
}
}
上面的例子会有一个很大的弊端,就是测试数据存放在测试步骤中,不利于用例数据的维护,可以改成以下方式,测试用例更加直观明了
Login.feature
Feature: Login Action
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
# And User enters username and password
And User enters "test" and "test123456"
Then Message displayed Login Successfully
Scenario: Successful LogOut
When User LogOut from the Application
Then Message displayed LogOut Successfully
LoginSteps.java
package steps;
import io.cucumber.java8.En;
public class LoginSteps implements En{
public LoginSteps() {
Given("^User is on Home Page$", () -> {
System.out.println("User is on Home Page");
});
When("^User Navigate to LogIn Page$", () -> {
System.out.println("User Navigate to LogIn Page");
});
Then("^Message displayed Login Successfully$", () -> {
System.out.println("Message displayed Login Successfully");
});
When("^User LogOut from the Application$", () -> {
System.out.println("User LogOut from the Application");
});
Then("^Message displayed LogOut Successfully$", () -> {
System.out.println("Message displayed LogOut Successfully");
});
And("^User enters "([^"]*)" and "([^"]*)"$", (String username, String password) -> {
System.out.println("username:" + username);
System.out.println("password:" + password);
});
}
}
使用Examples关键字
假设有一个Scenario需要测试一组不同的测试数据,这时我们Examples关键字,需要搭配Scenario Outline关键字一起使用,使用示例如下:
ScenarioOutline.feature
Feature: Scenario outline test
Scenario Outline: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters "" and ""
Then Message displayed Login Successfully
Examples:
| username | password |
| userA | passwordA |
| userB | passwordB |
测试结果如下:
测试结果可以看出Scenario循环跑了两次,因为Scenario配置了两组测试数据 。
Cucumber的数据表格
上面的Examples关键字的例子中,测试数据是对当前Scenario的所有步骤生效,如果只需要对单个步骤传数据,除了例子1中的方法,我们还可以使用Cucumber的数据表格,使用示例如下:
CucumberDateTables.feature
Feature: Scenario outline test
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters Credentials to LogIn
| testuser_1 | Test@153 |
Then Message displayed Login Successfully
Step实现
And("^User enters Credentials to LogIn$", (DataTable usercredentials) -> {
List> data = usercredentials.cells();
System.out.println(data.get(0).get(0));
System.out.println(data.get(0).get(1));
});
数据表格中使用Maps
如果我们需要对Scenario的某个步骤需要传数据,并且循环执行两次,我们应该怎么做呢?直接上代码:
StepRunTwoTime.feature
Feature: Datables use map
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters Credentials to LogIn Use map
| username | password|
| testuser_1 | Test@153 |
| testuser_2 | Test@188 |
Then Message displayed Login Successfully
Step实现
And("^User enters Credentials to LogIn Use map$", (DataTable usercredentials) -> {
for (Map data : usercredentials.asMaps(String.class, String.class)) {
System.out.println(data.get("username"));
System.out.println(data.get("password"));
}
});
测试结果:
上图可以看出测试步骤3循环跑了两次。
Github代码:LeechenLove/Cucumber-Example (github.com)
本文完。



