【cucumber】cucumber-reporting生成测试报告

原始的cucumber report 比较粗糙

我们可以通过cucumber-reporting 插件对报告进去优化

在pom.xml里面添加cuccumber-reporting 插件

XML 复制代码
 <!-- 根据 cucumber json文件 美化测试报告-->
        <dependency>
            <groupId>net.masterthought</groupId>
            <artifactId>cucumber-reporting</artifactId>
            <version>5.7.5</version>
        </dependency>

根据cuccumber-reporting创建一个工具类 reportUtils.java 生成报告:

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文件,再去生成美化后的报告。

java 复制代码
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目录下生成报告 测试报告样式:

相关推荐
哎呀呀嗯呀呀17 分钟前
class 031 位运算的骚操作
java·算法·位运算
2401_8581205320 分钟前
古典舞在线交流平台:SpringBoot设计与实现详解
java·spring boot·后端
大白飞飞27 分钟前
IDEA创建、导入、删除maven项目
java·maven·intellij-idea
赐你岁月如歌32 分钟前
如何使用ssm实现基于web的网站的设计与实现+vue
java·后端·ssm
2401_857297911 小时前
秋招内推2025-招联金融
java·前端·算法·金融·求职招聘
一 乐1 小时前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·数据库·学习·考研·微信·小程序·源码
一 乐1 小时前
租拼车平台|小区租拼车管理|基于java的小区租拼车管理信息系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·微信·notepad++·拼车
xmh-sxh-13142 小时前
如何选择数据库架构
java
jxxchallenger2 小时前
踩坑spring cloud gateway /actuator/gateway/refresh不生效
java·数据库·gateway
远望樱花兔2 小时前
【d59】【Java】【力扣】146.LRU缓存
java·开发语言·算法