【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目录下生成报告 测试报告样式:

相关推荐
Lhappy嘻嘻3 小时前
Java IO|File 文件操作 + 字节流 / 字符流完整笔记 + 递归删除文件实战
java·笔记·php
伊玛目的门徒4 小时前
试用leetcode之典中典 二数之和问题
java·算法·leetcode
懒鸟一枚7 小时前
深入理解 Linux 内存、Swap 交换分区与分页机制的关系
java·linux·数据库
我命由我123458 小时前
执行 Gradle 指令报错,无法将“grep”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
考虑考虑8 小时前
Sentinel安装
java·后端·微服务
凤凰院凶涛QAQ10 小时前
《Java版数据结构 & 集合类剖析》栈与队列:“push/pop 是栈的灵魂,offer/poll 是队列的骨架——四组 API,两种人生”
java·开发语言·数据结构
掘金_答案11 小时前
上线那天,一个 ConcurrentHashMap 差点送走我的 AI 客服——3 天排查 JVM 血泪史
java·后端·架构
猿与禅11 小时前
CosId 分布式 ID 生成器完全教程:从架构原理到生产落地
java·shardingsphere·雪花算法·分布式id·高性能·cosid·号段模式
MindUp11 小时前
企业网盘权限模型解析:多层级访问控制与审计能力选型指南
java·linux·运维
NG47711 小时前
【软件设计与体系结构】-策略设计模式
java·设计模式·软件工程