欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > Allure 和 JUnit 4结合学习

Allure 和 JUnit 4结合学习

2024/10/24 20:14:50 来源:https://blog.csdn.net/pumpkin84514/article/details/140579456  浏览:    关键词:Allure 和 JUnit 4结合学习

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 的注释,您可以清晰地记录测试步骤和断言,使测试报告更加详尽和易于理解

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com