Spring Boot Cucumber 测试报告嵌入方法

发布于:2025-07-15 ⋅ 阅读:(12) ⋅ 点赞:(0)

在Spring Boot项目中结合Cucumber进行测试时,将结果(如图片、JSON数据)嵌入到测试报告中,主要通过Cucumber的嵌入(Embedding)机制实现。以下是具体实现步骤和示例:


核心原理

Cucumber支持在步骤定义中通过Scenario对象附加(attach)任意二进制或文本数据,这些数据会被自动嵌入到生成的报告中(如HTML报告)。


实现步骤

1. 添加依赖

确保cucumber-javacucumber-spring依赖中包含报告模块:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>7.15.0</version>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>7.15.0</version>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId> <!-- 可选,依赖注入 -->
    <version>7.15.0</version>
</dependency>
2. 配置报告生成

@CucumberOptions中指定HTML报告格式:

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features",
    glue = "com.example.steps",
    plugin = {
        "pretty",
        "html:target/cucumber-report.html",  // 生成HTML报告
        "json:target/cucumber.json"          // 生成JSON报告(可选)
    }
)
public class RunCucumberTest {
//Powered by https://zhengkai.blog.csdn.net/
}

3. 嵌入数据到报告

在步骤定义中注入Scenario对象,使用attach()方法嵌入数据:

示例1:嵌入图片(如失败截图)
import io.cucumber.java.AfterStep;
import io.cucumber.java.Scenario;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.springframework.beans.factory.annotation.Autowired;

public class UISteps {

    @Autowired
    private WebDriver webDriver; // 假设已配置Selenium WebDriver

    @AfterStep
    public void captureScreenshotOnFailure(Scenario scenario) {
        if (scenario.isFailed()) {
            // 截取屏幕为字节数组
            byte[] screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BYTES);
            // 嵌入图片到报告(MIME类型指定为image/png)
            scenario.attach(screenshot, "image/png", "Failure Screenshot");
        }
    }
}
示例2:嵌入JSON数据
import io.cucumber.java.en.Then;
import io.cucumber.java.Scenario;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ApiSteps {

    @Then("验证API返回的JSON")
    public void verifyApiResponse(Scenario scenario) throws Exception {
        // 模拟获取API响应
        ApiResponse response = callSomeApi();
        
        // 将对象转为JSON字符串
        String json = new ObjectMapper().writeValueAsString(response);
        
        // 嵌入JSON文本到报告(MIME类型指定为application/json)
        scenario.attach(json, "application/json", "API Response");
    }
}

报告效果

  1. HTML报告

  2. JSON报告
    嵌入内容会以Base64编码存储在JSON中:

    "embeddings": [
      {
        "mime_type": "image/png",
        "data": "iVBORw0KGgoAAAANSUhEUgAA...",
        "name": "Failure Screenshot"
      }
    ]

关键注意事项

  1. attach()方法参数

    scenario.attach(data, mimeType, description);
    • data: 支持byte[]StringInputStream

    • mimeType: 如 "image/png""application/json""text/plain"

    • description: 报告中显示的标题

  2. 钩子函数选择

    • @AfterStep:每一步执行后嵌入(适合截图)

    • @After:每个场景执行后嵌入

    • 直接在@Given/@When/@Then步骤方法中嵌入

  3. 大文件处理

    • 避免嵌入超大文件(>10MB),可能影响报告打开速度

    • 建议使用外部存储链接代替:

      scenario.attach("https://example.com/screenshot.png", "text/uri-list", "External Screenshot");
  4. Spring上下文注入
    确保步骤类被Spring管理(使用@Component注解),并开启Cucumber-Spring集成:

    @SpringBootTest(classes = DemoApplication.class)
    public class SpringIntegrationTest {}

完整项目结构

src/test/
├── java/
│   ├── com/example/
│   │   ├── RunCucumberTest.java         // Cucumber 运行器
│   │   ├── steps/
│   │   │   ├── UISteps.java             // UI步骤定义
│   │   │   └── ApiSteps.java            // API步骤定义
│   │   └── config/
│   │       └── SpringConfig.java        // Spring测试配置
│   └── resources/
│       └── features/
│           └── login.feature            // 特性文件

通过以上实现,测试执行后可在target/cucumber-report.html中查看包含嵌入数据的完整报告。


网站公告

今日签到

点亮在社区的每一天
去签到