1. BDD
BDD(Behaviour-Driven Development)是软件团队缩小业务人员和技术人员之间差距的一种方式,关注于系统的整体行为,类似我们常说的验收测试。
BDD方式的优势:
鼓励跨角色协作,以建立对要解决的问题的共享理解
通过快速、小的迭代来增加反馈和价值流
生成针对系统行为自动检查的系统文档
2. Cucumber
Cucumber是支持BDD方式的一个工具.
Cucumber读取以纯文本编写的可执行规范,并验证软件是否按照这些规范执行。 规范由多个示例或场景组成。 例如:
Feature: Login
Scenario: Test login by username and password
Given A register username and it's password
When Input username and password
And Click login button
And Wait the loading dispeare
Then Direct to Home page
And Show login sucessful tips
每个场景由一系列的步骤组成。Cucumber验证软件是否符合规范,并为每个场景生成使用 ✅标明成功❌ 标明失败的报告。
3. Gherkin
Gherkin是一组语法规则,它使纯文本的结构足以让Cucumber理解。 上面的场景是用Gherkin写的。
使用Gherkin的目的:
- 明确的可执行的规范
- 使用Cucumber进行自动化测试
- 记录系统的实际行为
Cucumber语法以不同的形式存在于许多口语中,团队里可以使用团队语言中的关键字。
Gherkin 文档以 .feature
文件结尾,并且通常与软件一起在源代码控制中进行版本控制。
4. Step 定义
Step 定义把使用Gherkin编写的step关联到程序代码中。 步骤定义执行应该由步骤执行的操作。 因此,步骤定义将规范与实现连接起来。
5. 代码
新建maven工程,引入cucumber依赖
<properties>
<cucumber.version>6.8.1</cucumber.version>
</properties>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
定义Feature文件,如分别定义Login.feature 跟Logout.feature文件。
Feature: Login
Scenario: Test login by username and password
Given A register username "testName", and it's password "testPassword"
When Input username and password
And Click login button
And Wait the loading dispeare
Then Direct to Home page
And Show login sucessful tips
Feature: Logout
Scenario: Logout
Given A login user
When Click logout button
Then Direct to login page
And Show logout successful tips
在如IntelliJ Idea中安装插件,使用Alt+Enter 生成对应的Step(Java):
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.Assert;
public class MyStepdefs {
@Given("A register username {string}, and it's password {string}")
public void aRegisterUsernameAndItSPassword(String testName, String testPassword) {
System.out.println(testName);
System.out.println(testPassword);
}
@When("Input username and password")
public void inputUsernameAndPassword() {
System.out.println("1");
Assert.assertTrue(true);
}
@And("Click login button")
public void clickLoginButton() {
System.out.println("1");
}
@Then("Direct to Home page")
public void directToHomePage() {
System.out.println("1");
}
@And("Show login sucessful tips")
public void showLoginSucessfulTips() {
System.out.println("1");
}
@Given("A login user")
public void aLoginUser() {
}
@When("Click logout button")
public void clickLogoutButton() {
}
@Then("Direct to login page")
public void directToLoginPage() {
}
@And("Show logout successful tips")
public void showLogoutSuccessfulTips() {
}
@And("Wait the loading dispeare")
public void waitTheLoadingDispeare() {
}
}
Run test(使用Junit):
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import java.io.IOException;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {
"pretty",
"html:target/cucumber-html-report.html",
"json:target/cucumber-json-report/cucumber.json"
},
features = "src/test/resources",
tags = "not @ignore", monochrome=true)
public class RunTest {
@BeforeClass()
public static void setUp() throws IOException, InterruptedException {
}
}
报告
跑完:
查看结果,报告文件有json跟html格式
还可以集成到CICD工具,如Jenkins,定时或指定条件下触发case并生成报告。
6. 更多用法
变量
Feature: DataTable
Scenario: Test Datatable
Given a client "clientId"
And I prepare data as input:
|name | testName |
|age | 20 |
|Address | Guangzhou|
And I prepare data as below:
| firstName | lastName | birthDate |
| Annie M. G. | Schmidt | 1911-03-20 |
| Roald | Dahl | 1916-09-13 |
| Astrid | Lindgren | 1907-11-14 |
String入参
@Given("a client {string}")
public void aClient(String arg0) {
System.out.println(arg0);
}
##输出:clientId
Map入参
@And("I prepare data as input:")
public void iPrepareDataAsInput(Map<String,String> tableData) {
System.out.println(tableData);
}
##输出:{name=testName, age=20, Address=Guangzhou}
Map list入参
@And("I prepare data as below:")
public void iPrepareDataAsBelow(List<Map<String,String>> tableDataList) {
tableDataList.forEach(t->{
System.out.println(t);
});
}
##输出:
{firstName=Annie M. G., lastName=Schmidt, birthDate=1911-03-20}
{firstName=Roald, lastName=Dahl, birthDate=1916-09-13}
{firstName=Astrid, lastName=Lindgren, birthDate=1907-11-14}
Scenario Outline
使用Scenario Outline可以使用Examples把类似的Scenario归在一起,让Case更简洁
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 |
7. 小结
介绍了Cucumber工具主要是用来支持BDD方式,Cucumber工具使用Gherkin语法编写.feature文件,在.feature文件里编写Step与测试代码关联起来,这样把产品需求的user story与user case做了个很好的关联,让业务、开发、测试对系统的业务流程有统一的认识与理解。