Allure测试报告

github 地址:

https://github.com/allure-framework/allure2

源码下载地址:

https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline

源码配置安装

  • vi .bash_profile ,然后添加:
# Allure
export ALLURE_HOME=~/Library/allure-2.21.0
export PATH=$PATH:$ALLURE_HOME/bin
  • source ~/.bash_profile是环境变量生效

maven插件安装:pom.xml文件


    <properties>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <maven.compiler.version>3.10.1</maven.compiler.version>
        <maven-surefire-plugin.version>3.0.0-M9</maven-surefire-plugin.version>
        <!-- 使用 Java 17 语言特性 ( -source 11 )  -->
        <java.version>17</java.version>
        <!-- 对应junit Jupiter的版本号;放在这里就不需要在每个依赖里面写版本号,导致对应版本号会冲突 -->
        <junit.jupiter.version>5.9.2</junit.jupiter.version>
        <!-- log日志 -->
        <slf4j.version>2.0.6</slf4j.version>
        <logback.version>1.4.5</logback.version>
        <!-- yaml对应解析 -->
        <jackson.version>2.14.2</jackson.version>
        <!-- hamcrest断言 -->
        <hamcrest.version>2.2</hamcrest.version>
        <!-- allure报告 -->
        <allure.version>2.21.0</allure.version>
        <allure.maven.version>2.12.0</allure.maven.version>
        <aspectj.version>1.9.19</aspectj.version>
        <allure.cmd.download.url>
            https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline
        </allure.cmd.download.url>
    </properties>
    <!--    物料清单 (BOM)-->
    <dependencyManagement>
        <dependencies>
            <!--当使用 Gradle 或 Maven 引用多个 JUnit 工件时,此物料清单 POM 可用于简化依赖项管理。不再需要在添加依赖时设置版本-->
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>${junit.jupiter.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- junit5 -->
        <!-- 创建 Junit5 测试用例的 API-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <!--对应添加的依赖的作用范围-->
            <scope>test</scope>
        </dependency>
        <!-- 兼容 JUnit4 版本的测试用例-->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <!--suite套件依赖 -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- log日志 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>compile</scope>
        </dependency>

        <!--        allure报告-->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-junit5</artifactId>
            <version>${allure.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>

        <!--        yaml文件解析-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>${jackson.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <!-- 设置编码为 UTF-8 -->
                    <encoding>${maven.compiler.encoding}</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                     <argLine>
                      -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                     </argLine>
                    <includes>
                        <!--  <include>**/*Test</include>-->
                        <!--  <include>**/Test*</include>-->
                    </includes>
                    <systemProperties>
                        <property>
                            <name>allure.results.directory</name>
                            <value>${project.build.directory}/allure-results</value>
                        </property>
                    </systemProperties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${junit.jupiter.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                        <version>${junit.jupiter.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <!-- gradle 则添加allure-gradle依赖-->
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-maven</artifactId>
                <version>${allure.maven.version}</version>
                <configuration>
                    <reportVersion>${allure.version}</reportVersion>
                    <allureDownloadUrl>${allure.cmd.download.url}/${allure.version}/allure-commandline-${allure.version}.zip</allureDownloadUrl>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

报告生成

  • 静态资源生成方式:
    1.mvn clean test allure:report
    2.1.allure open target/site/allure-maven-plugin -p 4444
    2.2.mvn allure:serve
  • 在线report生成:
    1.mvn clean test
    2.allure generate target/allure-results -o ./report -p 4444 --clean
    3.allure open ./report
    allure-results文件地址:mvn执行之后位置:target/allure-results,idea执行之后位置:allure-result

报告中添加用例步骤:

  • 使用allure.step()添加
  • 使用 @Step注解
@DisplayName("步骤方法验证")
public class StepMethodTest {
    int result;

    // Allure.step()添加用例步骤
    @Test
    @DisplayName("加法步骤验证")
    void testSum() {
        Allure.step("输入数字 3");
        int a = pressDigit(3);
        Allure.step("输入加号");
        String str = pressAddition();
        Allure.step("输入数字 2");
        int b = pressDigit(2);
        Allure.step("点击等号");
        result = sum(a, b);
        Allure.step("验证结果是否正确");
        assertEquals(5, result, a + str + b + "计算错误");
    }

    private int pressDigit(int digit) {
        return digit;
    }

    private String pressAddition() {
        return "+";
    }

    public int sum(int... numbers) {
        return IntStream.of(numbers).sum();
    }
    
    // @Step注解添加用例步骤
    @Test
    @DisplayName("减法步骤验证")
    void testSub() {
        int a =  pressDigit(6);
        String str = pressSubtraction();
        int b = pressDigit(2);
        result = subtract(a, b);
        assertEquals(4, result, a + str + b + "计算错误");
    }

    @Step("输入数字 {digit}")
    private int pressDigit(int digit) {
        return digit;
    }
    @Step("输入减号")
    private String pressSubtraction() {
        return "-";
    }

    @Step("减法计算")
    public int subtract(int x, int y) {
        return x-y;
    }
}

添加用例连接

  • 使用注解方式添加链接
    1、@Link注解:@Link(name = "链接名称", url = "链接地址", type = "链接类型")
    2、@Issue注解:@Issue("链接地址")

  • 使用方法添加连接:
    1、Allure.link(String name, String url, LinkType type);
    2、Allure.issue(String name, String url);

添加用例分类

  • 系统模块: @Epic/@Epics
  • 主功能模块: @Feature/@Features
  • 细分模块: @Story/@Stories
  • 关系: @Epic@Feature@Story

用例优先级分类

  • @Sverity

支持用例的tags标签

失败重试的方法:

mvn clean test allure:report -Dsurefile.rerunFailingTestsCount=3

报告中添加附件-图片

  • 注解方式添加。
    @Attachment(value = "图片名", type = "image/png", fileExtension = "后缀")

  • 调用方法添加。
    Allure.Attachment("图片名", "image/png",图片路径, "后缀");

public class PngTest{
    @Test
    @DisplayName("allure图片注解的添加")
    public void testAddPngWithAttachmentAn() throws IOException {
       // byte[] contents = Files.newInputStream(Paths.get("allure.png")).readAllBytes();//jdk11
        byte[] contents = Files.readAllBytes(Paths.get("src/test/resources/allure.png"));//jdk8
        attachPngFile(contents,"allure图");
    }

    @Attachment(value = "{pngName}", type="image/png", fileExtension="png")
    public byte[] attachPngFile(byte[] contents,String pngName){
        System.out.println("png"+pngName);
        return contents;
    }

    @Test
    @DisplayName("allure图片方法的添加")
    public void testAddPngWithAttachmentMethod() throws IOException{
        Allure.addAttachment("截图","image/png",
                Files.newInputStream(Paths.get("src/test/resources/allure.png")),"png");
    }
}

报告中添加附件-日志

  • 注解方式添加。
    @Attachment(value = "文本文件名", type = "text/plain", fileExtension = ".txt")
  • 调用方法添加。
    Allure.Attachment("文本文件名", "text/palin",文本文件路径, ".txt");
public class textTest{
    @Test
    @DisplayName("注解:String类型")
    public void testAddTextForStringWithAn() throws IOException {
        File file=new File("src/test/resources/a.txt");
        BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
        StringBuilder sb=new StringBuilder();
        String line;
        while((line=bufferedReader.readLine()) !=null){
            sb.append(line);
            sb.append(System.lineSeparator());
        }
        attachTextFile(sb.toString(),"text文本");
    }

    @Attachment(value = "{fileName}",type="text/plain")
    public String attachTextFile(String contents,String fileName){

        return contents;
    }

    @Test
    @DisplayName("注解:String类型")
    public void testAddTextForStringWithbyte() throws IOException{
        byte[] contents= Files.readAllBytes(Paths.get("src/test/resources/a.txt"));
        attachTextFileWithByte(contents,"byte数组添加文本");
    }

    @Attachment(value = "{fileName}",type="text/plain")
    public byte[] attachTextFileWithByte(byte[] contents,String fileName){

        return contents;
    }

    @Test
    @DisplayName("text文本添加")
    public void testAddPngWithAttachmentMethod() throws IOException{
        File file=new File("src/test/resources/a.txt");
        BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
        StringBuilder sb=new StringBuilder();
        String line;
        while((line=bufferedReader.readLine()) !=null){
            sb.append(line);
            sb.append(System.lineSeparator());
        }
        Allure.addAttachment("方法添加String文本","text/plain",sb.toString(),".txt");
    }

    @Test
    @DisplayName("text文本通过Stream流添加")
    public void testAddPngWithAttachmentStream() throws IOException{
        Allure.addAttachment("text文本通过Stream流添加","text/plain",
                Files.newInputStream(Paths.get("src/test/resources/a.txt")),".txt");
    }
}

报告中添加附件-日志

  • 注解形式添加: @Attachment(value = "连接名", type = "text/html", fileExtension = ".html")
  • 方法形式添加: Allure.Attachment("连接名", "text/html",index.html路径, ".html");
public class HtmlTest {
    @Test
    @DisplayName("html 注解形式添加")
    public void testHtmlWithAn() throws IOException {
        File file=new File("src/test/resources/index.html");
        BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
        StringBuilder sb=new StringBuilder();
        String line;
        while((line=bufferedReader.readLine()) !=null){
            sb.append(line);
            sb.append(System.lineSeparator());
        }
        attachHtmlFile(sb.toString(),"首页注解");
    }

    @Attachment(value = "{htmlName}",type = "text/html")
    private String attachHtmlFile(String contents, String htmlName) {
        return contents;
    }

    @Test
    @DisplayName("html方法形式添加")
    public void testWithMethodAndHTML() throws IOException {
        Allure.addAttachment("首页","text/html", Files.newInputStream(Paths.get("src/test/resources/index.html")),".html");
    }
}

报告中添加附件-视频

  • 注解形式添加: @Attachment(value = "视频名", type = "video/mp4", fileExtension = ".mp4")
  • 方法形式添加: Allure.Attachment("视频名", "video/mp4",视频路径, ".mp4");
public class Mp4Test {
    @Test
    @DisplayName("allure注解添加的视频")
    public void testAddPngWithAttachmentAn() throws IOException {
        // byte[] contents = Files.newInputStream(Paths.get("allure.png")).readAllBytes();//jdk11
        byte[] contents = Files.readAllBytes(Paths.get("src/test/resources/allure.mp4"));//jdk8
        attachPngFile(contents,"注解添加视频");
    }

    @Attachment(value = "{mp4Name}", type="video/mp4", fileExtension=".mp4")
    public byte[] attachPngFile(byte[] contents,String mp4Name){
        System.out.println("mp4"+mp4Name);
        return contents;
    }

    @Test
    @DisplayName("allure方法添加的视频")
    public void testAddPngWithAttachmentMethod() throws IOException{
        Allure.addAttachment("方法添加视频","video/mp4",
                Files.newInputStream(Paths.get("src/test/resources/allure.mp4")),"mp4");
    }
}

报告定制

  • 页面logo
    1.修改allure.yml文件,添加logo插件custom-logo-plugin(在allure安装路径下)。
    2.编辑styles.css文件,配置logo图片。目录在:/xxx/allure-2.13.2/plugins/custom-logo-plugin/static/styles.css,
  • 页面标题:编辑 styles.css 文件,添加修改标题对应的代码。
.side-nav__brand {
  /*background: url('custom-logo.svg') no-repeat left center !important;*/
  background: url('kyzhpum201w3.png') no-repeat left center !important;
  margin-left: 10px;
  height: 40px;
  background-size: contain !important;
}
/* 去掉图片后边 allure 文本 */
.side-nav__brand-text {
  display: none;
}

/* 设置logo 后面的字体样式与字体大小
  content: 显示的文本内容
  margin-left: 距离左侧图片位置长度
  height: 文本高度
  font-family: 字体
  font-size: 字体大小
 */
.side-nav__brand:after {
  content: "霍格沃兹测试开发学社";
  margin-left: 10px;
  height: 20px;
  font-family: Arial, serif;
  font-size: 10px;
}