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

相关推荐

  1. Allure JUnit 4结合学习

    2024-07-21 13:14:01       19 阅读
  2. 浅谈单元测试JUnit4使用

    2024-07-21 13:14:01       33 阅读
  3. JUnit 4单元测试常用注解方法

    2024-07-21 13:14:01       26 阅读
  4. Spring整合Junit4

    2024-07-21 13:14:01       43 阅读
  5. <span style='color:red;'>JUnit</span>

    JUnit

    2024-07-21 13:14:01      45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 13:14:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 13:14:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 13:14:01       45 阅读
  4. Python语言-面向对象

    2024-07-21 13:14:01       55 阅读

热门阅读

  1. vue3 学习笔记17 -- echarts的使用

    2024-07-21 13:14:01       22 阅读
  2. GPT-5一年半后发布

    2024-07-21 13:14:01       17 阅读
  3. 批量下载网易云音乐歌单的Python脚本

    2024-07-21 13:14:01       22 阅读
  4. 力扣1834.单线程CPU

    2024-07-21 13:14:01       20 阅读
  5. from lxml import etree 的功能

    2024-07-21 13:14:01       17 阅读
  6. (四)js前端开发中设计模式之工厂方法模式

    2024-07-21 13:14:01       21 阅读