Allure 和 JUnit 4结合
什么是 Allure
Allure 是一个灵活的、开源的测试报告工具,可以帮助生成详细、可读的测试报告。它能够集成多种测试框架(如 JUnit、TestNG、Cucumber 等)和 CI/CD 工具(如 Jenkins、TeamCity 等),提供对测试执行过程的详细视图,包括测试步骤、断言、测试结果等。
Allure 注释
Allure 提供了一些注释,用于标记测试用例和测试步骤。常用的注释包括:
@Description
: 用于添加对测试用例或测试方法的描述。@Step
: 用于标记测试步骤,并提供详细的步骤描述。@Attachment
: 用于附加文件(如截图、日志等)到测试报告中。@Severity
: 用于标记测试用例的严重级别。@Epic
,@Feature
,@Story
: 用于组织和分类测试用例。
与 JUnit 4 结合使用
要将 Allure 与 JUnit 4 结合使用,需要在 Maven 项目中添加 Allure 和 JUnit 4 的依赖,并配置 Maven 插件以生成 Allure 测试报告。
步骤1:设置 Maven 项目
在 pom.xml
文件中添加 Allure 和 JUnit 4 的依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>my-app</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>io.qameta.allure</groupId><artifactId>allure-junit4</artifactId><version>2.13.9</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M5</version><configuration><redirectTestOutputToFile>true</redirectTestOutputToFile><systemPropertyVariables><allure.results.directory>${project.build.directory}/allure-results</allure.results.directory></systemPropertyVariables></configuration></plugin><plugin><groupId>io.qameta.allure</groupId><artifactId>allure-maven</artifactId><version>2.10.0</version><executions><execution><id>allure-report</id><phase>verify</phase><goals><goal>report</goal></goals></execution></executions></plugin></plugins></build>
</project>
步骤2:创建测试类并使用 Allure 注释
在 src/main/java/com/example/
目录下创建 Calculator.java
:
package com.example;public class Calculator {public static int add(int a, int b) {return a + b;}public static int subtract(int a, int b) {return a - b;}public static int multiply(int a, int b) {return a * b;}public static int divide(int a, int b) {if (b == 0) {throw new IllegalArgumentException("Divisor cannot be zero");}return a / b;}
}
在 src/test/java/com/example/
目录下创建 CalculatorTest.java
:
package com.example;import io.qameta.allure.Description;
import io.qameta.allure.Step;
import org.junit.Test;
import static org.junit.Assert.*;public class CalculatorTest {@Test@Description("测试两个正数的加法操作")public void testAddition() {int result = add(2, 3);checkResult("加法结果", 5, result);}@Test@Description("测试两个正数的减法操作")public void testSubtraction() {int result = subtract(5, 3);checkResult("减法结果", 2, result);}@Test@Description("测试两个正数的乘法操作")public void testMultiplication() {int result = multiply(2, 3);checkResult("乘法结果", 6, result);}@Test@Description("测试两个正数的除法操作")public void testDivision() {int result = divide(6, 3);checkResult("除法结果", 2, result);}@Test(expected = IllegalArgumentException.class)@Description("测试除以零的情况")public void testDivisionByZero() {divide(6, 0);}@Step("计算两个数的加法: {0} + {1}")private int add(int a, int b) {return Calculator.add(a, b);}@Step("计算两个数的减法: {0} - {1}")private int subtract(int a, int b) {return Calculator.subtract(a, b);}@Step("计算两个数的乘法: {0} * {1}")private int multiply(int a, int b) {return Calculator.multiply(a, b);}@Step("计算两个数的除法: {0} / {1}")private int divide(int a, int b) {return Calculator.divide(a, b);}@Step("检查 {0} 应该为 {1}")private void checkResult(String description, int expected, int actual) {assertEquals(description, expected, actual);}
}
步骤3:运行测试并生成报告
在项目根目录下,运行以下命令执行测试并生成 Allure XML 报告:
mvn test
步骤4:生成 HTML 报告
运行以下命令生成 Allure HTML 报告:
mvn allure:serve
生成的 XML 报告示例
生成的 XML 文件位于 target/allure-results
目录下,以下是一个示例 XML 文件的内容:
<ns2:test-suite xmlns:ns2="urn:model.allure.qatools.yandex.ru"><name>com.example.CalculatorTest</name><title>CalculatorTest</title><test-cases><test-case><name>testAddition</name><description>测试两个正数的加法操作</description><steps><step><name>计算两个数的加法: 2 + 3</name><status>passed</status><start>1625150400000</start><stop>1625150401000</stop></step><step><name>检查 加法结果 应该为 5</name><status>passed</status><start>1625150401000</start><stop>1625150402000</stop></step></steps><status>passed</status><start>1625150400000</start><stop>1625150402000</stop></test-case><test-case><name>testSubtraction</name><description>测试两个正数的减法操作</description><steps><step><name>计算两个数的减法: 5 - 3</name><status>passed</status><start>1625150402000</start><stop>1625150403000</stop></step><step><name>检查 减法结果 应该为 2</name><status>passed</status><start>1625150403000</start><stop>1625150404000</stop></step></steps><status>passed</status><start>1625150402000</start><stop>1625150404000</stop></test-case><!-- 更多测试用例 --></test-cases>
</ns2:test-suite>
通过以上步骤,您可以使用 Allure 和 JUnit 4 生成详细的测试报告,包括测试用例的描述和断言点。通过 Allure 的注释,您可以清晰地记录测试步骤和断言,使测试报告更加详尽和易于理解