原始的cucumber report 比较粗糙
我们可以通过cucumber-reporting 插件对报告进去优化
在pom.xml里面添加cuccumber-reporting 插件
<!-- 根据 cucumber json文件 美化测试报告-->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>5.7.5</version>
</dependency>
根据cuccumber-reporting创建一个工具类 reportUtils.java 生成报告:
package com.cacho.s2b.lesson.util;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;
import net.masterthought.cucumber.presentation.PresentationMode;
import net.masterthought.cucumber.sorting.SortingMethod;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @Description cucumber-reporting 美化测试报告
* @Author LingWei
* @date 2023/12/31
**/
public class reportUtils {
public void generateCucumberReport(){
String buildNumber = "Release 23.12";
String projectName = "Test Demo";
File reportOutputDirectory = new File("target");
List<String> jsonFiles = new ArrayList<>();
// 根据cucumber生成的原始json报告去生成测试报告
jsonFiles.add("target/json-report/run.json");
// 测试报告配置信息目录,项目名称
Configuration configuration = new Configuration(reportOutputDirectory,projectName);
// 测试报告版本
configuration.setBuildNumber(buildNumber);
// 测试报告展示模式
configuration.addPresentationModes(PresentationMode.EXPAND_ALL_STEPS);
configuration.addPresentationModes(PresentationMode.PARALLEL_TESTING);
// 排序方式设置
configuration.setSortingMethod(SortingMethod.ALPHABETICAL);
configuration.addClassifications("Platform","Windows 11");
configuration.addClassifications("Component","API Test");
configuration.addClassifications("Version","23.12");
configuration.addClassifications("User","Cacho");
// json文件和配置一起去生成报告
ReportBuilder reportBuilder = new ReportBuilder(jsonFiles,configuration);
reportBuilder.generateReports();
}
}
在测试入口类 ApiTest.java 里面添加@AfterClass,并调用reportUtils.java,用cucumber跑完测试后的json文件,再去生成美化后的报告。
package com.cacho.s2b.lesson;
import com.cacho.s2b.lesson.engine.ApiInfoHub;
import com.cacho.s2b.lesson.util.reportUtils;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import lombok.extern.slf4j.Slf4j;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
@Slf4j
@RunWith(Cucumber.class)
@CucumberOptions(
plugin ={"pretty","json:target/json-report/run.json"}, // 生成的json文件
features = {"classpath:features" }, //features路路径,
glue = "com.cacho.s2b.lesson", //步骤所在的包名
monochrome = true, //
tags = "@test" //指定标签,多标签:"标签1 or/and/and not 标签2"
)
public class ApiTest {
@BeforeClass
public static void beforeClass(){
ApiInfoHub apiEnv = ApiInfoHub.getInstance();
log.info("运行的环境是:{}",apiEnv.getEnvInfo().getDescription());
}
@AfterClass
public static void afterClass(){
reportUtils report = new reportUtils();
report.generateCucumberReport();
}
}
运行后在项目的target\cucumber-html-reports目录下生成报告 测试报告样式: