1、介绍
以Cucumber7、JUnit4、JDK1.8环境介绍
1.1、Cucumber框架
Cucumber框架是行为驱动(BDD)框架的一种,通过自然语言站在功能使用者视角,描述编写测试用例。
简单的来说就是通过feature文件编写脚本,脚本对应java写的方法,会有一个启动器配置对应的规则,按照指定规则执行对应的脚本。
官方文档:https://cucumber.io/docs/cucumber/
1.2、什么是BDD?(摘自官网)
BDD是软件团队的一种工作方式,通过以下方式缩小了业务人员和技术人员之间的差距:
鼓励跨角色协作,以建立对要解决的问题的共同理解
在快速、小的迭代中工作,以增加反馈和价值流动
生成系统文档,根据系统行为自动检查
2、Cucumber语法
2.1、步骤声明
#描述性关键字
Feature #描述这段脚本的特征,一般用于报告
Rule: #cucumber6以上支持,用于组合Feature下面的特定规则方案
Background #方法中重复的步骤,集中写在这个关键字下面
Example/Scenario #这是一个说明业务规则的具体示例。它由一系列步骤组成。
例子1:
Feature: Overdue tasks
Let users know when tasks are overdue, even when using other
features of the app
Rule: Users are notified about overdue tasks on first use of the day
Background:
Given I have overdue tasks
Example: First use of the day
Given I last used the app yesterday
When I use the app
Then I am notified about overdue tasks
Example: Already used today
Given I last used the app earlier today
When I use the app
Then I am not notified about overdue tasks
例子2:
Feature: Multiple site support
Only blog owners can post to a blog, except administrators,
who can post to all blogs.
Background:
Given a global administrator named "Greg"
And a blog named "Greg's anti-tax rants"
And a customer named "Dr. Bill"
And a blog named "Expensive Therapy" owned by "Dr. Bill"
Scenario: Dr. Bill posts to his own blog
Given I am logged in as Dr. Bill
When I try to post to "Expensive Therapy"
Then I should see "Your article was published."
Scenario: Dr. Bill tries to post to somebody else's blog, and fails
Given I am logged in as Dr. Bill
When I try to post to "Greg's anti-tax rants"
Then I should see "Hey! That's not your blog!"
Scenario: Greg posts to a client's blog
Given I am logged in as Greg
When I try to post to "Expensive Therapy"
Then I should see "Your article was published."
#步骤关键字
Given
When
Then
And
But #这些就是自然语言里的意思,协助我们写出更通俗的脚本
在查找对应声明的步骤时,不会考虑关键字,这意味着以下步骤重复
Given there is money in my account
Then there is money in my account
* #即和上一个关键字相同
# "*"的使用
Scenario: All done
Given I am out shopping
* I have eggs
Scenario Outline 和 Examples #配合使用,达到参数化效果,如果没有Example可以使用Scenario做简单标志,不过就起不到参数化的效果
Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
2.2、步骤参数
#文档字符串
#通过"""或者'''传入字符串
Given a blog post named "Random" with Markdown body
"""
Some Title, Eh?
===============
Here is the first paragraph of my blog post. Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
"""
# Data Tables(数据表)
#可以参考官方文档:https://github.com/cucumber/cucumber-jvm/tree/main/datatable
# 正则表达式和Cucumber表达式
# 正则表达式的用法就是:添加锚点(以 开头和结尾)或正斜杠 (),正则表达式的语法不做介绍
# Cucumber表达式参考官方文档https://github.com/cucumber/cucumber-expressions#readme
#Cucumber表达式:
@Given("I have {int} cukes in my belly")
public void i_have_n_cukes_in_my_belly(int cukes) {
}
#等同于正则表达式写法:
@Given("I have ^\d+/ cukes in my belly")
public void i_have_n_cukes_in_my_belly(int cukes) {
}
2.3、Hooks(场景挂钩)
标注在脚本对应的步骤上,例如
@After
public void closeBrowser() {
commonService.closeBrowser();
}
Before #在每个方案的第一步之前运行。
After #每个方案的最后一步之后运行
BeforeStep AfterStep #在步骤之前和之后调用的步骤挂钩
BeforeAll AfterAll #在在运行任何方案之前和之后调用的步骤挂钩
2.4、标签
通过标签可以更方便指定哪些脚本执行
标签可以放置在以下小黄瓜元素的上方:
Feature
Scenario
Scenario Outline
Examples
标签语法:
@fast | 标记为 的方案@fast |
@wip and not @slow | 标记的方案,未同时标记@wip@slow |
@smoke and @fast | 同时标记有 和 的方案@smoke@fast |
@gui or @database | 使用 或 标记的方案@gui@database |
2.5、选项
参考文档:https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine#configuration-options
可以看这个例子,配合选项和启动器,就可以启动对应的脚本执行
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = { //格式化插件
"pretty",
"html:target/html-report.html",
"json:target/json-report/run.json"
},
features = {
"src\\test\\resources" //feature路径
},
glue = "step", //步骤所在的包名
monochrome = false,
tags = "@test" //指定标签,多标签:"标签1 or/and/and not 标签2"
)
public class hsjcRunTest {
}