目录
1、谷歌浏览器查看用户配置路径
地址栏输入:chrome:version
2、feature关键字中英文映射
中文编写feature文件,需要在文件第一行写上:language:zh-CN
feature | 功能 |
background | 背景 |
scenario | 场景 |
scenario_outline | 场景大纲 |
example | 例子 |
given | 假如 |
then | 那么 |
and | 而且 |
but | 但是 |
when | 当 |
3、cucumber测试报告
执行测试用例时,Cucumber默认在console中输出测试结果,当然也可以根据需要,通过配置Cucumber Options的Plugin参数,修改报告输出格式,支持同时输出多种格式的测试报告。配置如下:
@RunWith(Cucumber.class)
@CucumberOptions(features = ("features"),
glue = ("stepDefinitions"),
plugin = {"html:target/cucumber-reports/html",
"json:target/cucumber-reports/cucumber.json",
"junit:target/cucumber-reports/cucumber.xml",
"pretty"})
public class CucumberRunner {
}
1). Pretty Report
默认格式
2). HTML Reports
配置为"html:target/cucumber-reports/html",注意后面是文件夹
测试报告打开后如下:
3). JSON Report
配置为"json:target/cucumber-reports/cucumber.json"
[
{
"line": 1,
"elements": [
{
"line": 4,
"name": "使用百度搜索 什么是BDD",
"description": "",
"id": "百度搜索测试;使用百度搜索-什么是bdd",
"type": "scenario",
"keyword": "Scenario",
"steps": [
{
"result": {
"duration": 18420454819,
"status": "passed"
},
"line": 5,
"name": "打开百度首页",
"match": {
"location": "TestBaidu.打开百度首页()"
},
"keyword": "Given "
},
{
"result": {
"duration": 806864364,
"status": "passed"
},
"line": 6,
"name": "输入关键字 什么是BDD",
"match": {
"location": "TestBaidu.输入关键字_什么是BDD()"
},
"keyword": "And "
},
{
"result": {
"duration": 15208647753,
"error_message": "java.lang.AssertionError: 没有找到提交按钮!!!\r\n\tat org.junit.Assert.fail(Assert.java:88)\r\n\tat generalPage.BaiduHomepage.clickSubmitButton(BaiduHomepage.java:58)\r\n\tat stepDefinitions.TestBaidu.点击搜索(TestBaidu.java:36)\r\n\tat ✽.When 点击搜索(TestBaidu.feature:7)\r\n",
"status": "failed"
},
"line": 7,
"name": "点击搜索",
"match": {
"location": "TestBaidu.点击搜索()"
},
"keyword": "When "
},
{
"result": {
"status": "skipped"
},
"line": 8,
"name": "返回正确的搜索结果",
"match": {
"location": "TestBaidu.返回正确的搜索结果()"
},
"keyword": "Then "
}
],
"tags": [
{
"line": 3,
"name": "@search_01"
}
]
}
],
"name": "百度搜索测试",
"description": "",
"id": "百度搜索测试",
"keyword": "Feature",
"uri": "TestBaidu.feature"
}
]
4). JUNIT XML Report
配置为"junit:target/cucumber-reports/cucumber.xml",注意开头为junit
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<testsuite failures="1" name="cucumber.runtime.formatter.JUnitFormatter" skipped="0" tests="1" time="34.435967">
<testcase classname="百度搜索测试" name="使用百度搜索 什么是BDD" time="34.435967">
<failure message="java.lang.AssertionError: 没有找到提交按钮!!! 	at org.junit.Assert.fail(Assert.java:88) 	at generalPage.BaiduHomepage.clickSubmitButton(BaiduHomepage.java:58) 	at stepDefinitions.TestBaidu.点击搜索(TestBaidu.java:36) 	at ✽.When 点击搜索(TestBaidu.feature:7) "><![CDATA[Given 打开百度首页................................................................passed
And 输入关键字 什么是BDD............................................................passed
When 点击搜索...................................................................failed
Then 返回正确的搜索结果..............................................................skipped
StackTrace:
java.lang.AssertionError: 没有找到提交按钮!!!
at org.junit.Assert.fail(Assert.java:88)
at generalPage.BaiduHomepage.clickSubmitButton(BaiduHomepage.java:58)
at stepDefinitions.TestBaidu.点击搜索(TestBaidu.java:36)
at ✽.When 点击搜索(TestBaidu.feature:7)
]]></failure>
</testcase>
</testsuite>
4、使用插件美化测试报告
推荐使用cucumber-report插件,可根据原生的json测试报告来生成HTML格式的较美观的测试报告,且支持按features、tags、steps、failures来查看报告。(参考文档:https://www.ibm.com/developerworks/cn/java/j-lo-cucumber01/index.html)
- 1.安装:pom文件添加依赖:
<dependencies>
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
- 2.使用:创建java类,美化测试报告:
File reportOutputDirectory = new File("target");
List<String> jsonFiles = new ArrayList<>();
jsonFiles.add("cucumber-report-1.json");
jsonFiles.add("cucumber-report-2.json");
String buildNumber = "1";
String projectName = "cucumberProject";
boolean runWithJenkins = false;
Configuration configuration = new Configuration(reportOutputDirectory, projectName);
// optional configuration - check javadoc
configuration.setRunWithJenkins(runWithJenkins);
configuration.setBuildNumber(buildNumber);
// addidtional metadata presented on main page
configuration.addClassifications("Platform", "Windows");
configuration.addClassifications("Browser", "Firefox");
configuration.addClassifications("Branch", "release/1.0");
// optionally add metadata presented on main page via properties file
List<String> classificationFiles = new ArrayList<>();
classificationFiles.add("properties-1.properties");
classificationFiles.add("properties-2.properties");
configuration.addClassificationFiles(classificationFiles);
ReportBuilder reportBuilder = new ReportBuilder(jsonFiles, configuration);
Reportable result = reportBuilder.generateReports();
// and here validate 'result' to decide what to do if report has failed
- 3.生成样式:
按feature查看:
按tags查看:
按steps查看:
按failures查看: