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

本文介绍了如何通过cucumber-reporting插件美化Cucumber测试报告,包括在pom.xml中添加依赖,创建reportUtils.java工具类生成报告,并在测试入口类ApiTest中集成@AfterClass方法调用该工具进行报告生成。
摘要由CSDN通过智能技术生成

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

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cucumber是一种行为驱动开发(BDD)工具,它结合了自然语言和自动化测试,用于编写可执行的规范文档。它使用Gherkin语言来描述应用程序的行为,并将这些描述转化为可执行的测试代码。 Cucumber的核心思想是通过定义特定的场景和步骤来描述应用程序的行为。这些场景和步骤以Gherkin语言的格式编写,例如: ``` Feature: 用户登录 用户可以通过用户名和密码登录系统 Scenario: 正确的用户名和密码 Given 用户打开登录页面 When 用户输入正确的用户名和密码 And 用户点击登录按钮 Then 用户成功登录系统 Scenario: 错误的用户名和密码 Given 用户打开登录页面 When 用户输入错误的用户名和密码 And 用户点击登录按钮 Then 用户收到错误提示信息 ``` 上述示例中,`Feature`定义了一个功能,`Scenario`定义了一个具体的场景,`Given`、`When`、`Then`等关键词定义了具体的步骤。 Cucumber支持多种编程语言,如Java、Ruby、JavaScript等。你可以使用相应的Cucumber库来编写测试代码,并将Gherkin语言的描述与测试代码关联起来。当运行测试时,Cucumber会解析Gherkin语言的描述,并执行相应的测试代码。 Cucumber的优点包括: 1. 提供了一种可读性强的测试文档形式,使得非技术人员也能理解和参与测试过程。 2. 通过自然语言描述测试场景,减少了测试用例的维护成本。 3. 支持多种编程语言和测试框架,灵活性高。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值