Allure生成测试报告

  • Allure是一个轻量级,灵活的,支持多语言的测试报告工具
  • 多平台的,奢华的测试报告框架
  • 可以为dev/qa提供详尽的测试报告,测试步骤,log
  • 也可以为管理层提供high level的统计报告
  • java语言开发的,支持pytest,js,php,ruby等
  • 可以集成到Jenkins

一,下载对应的包&配置环境变量

1.下载allure
https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.13.2/
ps:这里注意,不要下2.13.10版本,这个打开会空白!!!

2. 配置环境变量:

  • jdk1.8版本及以上
  • 在path那边添加环境变量,比如D:\java课程\allure-commandline-2.13.2\allure-2.13.2\bin,配置完成后可以cmd输入allure --version验证是否配置成功

二. 添加依赖

这里匹配的是.Test结尾的java文件

<?xml version="1.0" encoding="UTF-8"?>
<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.hogwarts</groupId>
    <artifactId>AllureDemo2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-junit5</artifactId>
            <version>2.13.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-console-standalone</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest</artifactId>
            <version>2.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
     <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <aspectj.version>1.8.10</aspectj.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

allure常用命令

  • allure --help 帮助
  • allure --version 查看版本信息
  • allure serve 生成在线版本的测试报告,可以通过这个来解析最终的xUnit生成的结果
  • allure generate <allure-result中间文件> -o 输出目录(默认路径:allure report)

三,编写测试case并运行

在test/java下新建测试case

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class Demo1Test {
    @Test
    public void demo1() {
        assert 1 + 1 == 2;
    }

    @Test
    public void demo2() {
        assertThat(2, equalTo(2));
    }

    @Test
    public void demo3() {
        assertThat(2, equalTo(2));
    }
}

四. 使用allure工具解析测试报告

然后点击pom文件 - 右键 - open in terminal - 输入mvn clean test命令清空target目录下的surefile-reports


在这里插入图片描述

运行完上面的命令后,因为我们在pom文件配置了surfile,使用我们在命令行输入allure serve ./target/surefile-reports生成报告,但是这个我这测试时候不行,只能用后面的allure serve allure-results(这个想要是最新的需要手动删除allure-results目录),会自动跳转网页查看报告

如果只想执行单个case,直接输入
mvn clean -Dtest=com.hogwarts.demo.Demo2Test test

常用注解

在这里插入图片描述

演示几个常用注解:

  • 用例名称和描述展示
import io.qameta.allure.Allure;
import io.qameta.allure.Description;
import org.junit.Assert;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
 * @DisplayName 在外层显示case名称
 * @Description:对case的具体描述,在详情里展示,是静态描述信息
 * Allure.description:测试动态描述信息
 */
public class AllureDisplayAndDescriptionTest {
    @Test
    @DisplayName("测试显示名称")
    @Description("这是一个case的静态描述信息")
    void test1(){
        Assert.assertEquals(1,1);
    }

    @Test
    @DisplayName("测试动态描述信息")
    void test2(){
        Assert.assertEquals(1,1);
        Allure.description("11111");
        Assert.assertEquals(1,1);
        // 动态描述信息(写多个的时候只会展示最后一次添加的
        Allure.description("22222");
    }
}

展示如下


在这里插入图片描述
  • 添加链接
import io.qameta.allure.Allure;
import io.qameta.allure.Link;
import io.qameta.allure.Links;
import org.junit.jupiter.api.Test;

/**
 * 添加链接
 * 2种:
 * 内部链接:@Link
 * 外部链接:Allure.link("跳转到百度","https://www.baidu.com");
 */
public class AllureLinkTest {
    @Test
    @Link(name = "跳转到百度",url = "https://www.baidu.com")
    void link1(){
        System.out.println("外部链接");
    }

    @Test
    void link2(){
        System.out.println("内部链接");
        Allure.link("跳转到百度","https://www.baidu.com");
    }

    // 添加多个链接
    @Test
    @Links({
            @Link(name = "link 1",url = "https://www.baidu.com"),
            @Link(name = "link 2",url = "https://www.ceshiren.com"),
    })
    void multiLinks(){
        System.out.println("添加多个链接");
    }
}

展示如下:


在这里插入图片描述
  • 用例等级


    在这里插入图片描述
import io.qameta.allure.Severity;
import io.qameta.allure.SeverityLevel;
import org.junit.jupiter.api.Test;

/**
 * 等级划分执行顺序:
 * 依次是blocker>critical>normal>minor>trivial
 */
public class AllureLevelTest {
    @Test
    @Severity(SeverityLevel.BLOCKER)
    void blockerDemo(){
        System.out.println("BLOCKER");
    }
    @Test
    @Severity(SeverityLevel.CRITICAL)
    void criticalDemo(){
        System.out.println("CRITICAL");
    }
    @Test
    @Severity(SeverityLevel.MINOR)
    void minorDemo(){
        System.out.println("MINOR");
    }
    @Test
    @Severity(SeverityLevel.NORMAL)
    void normalDemo(){
        System.out.println("NORMAL");
    }
    @Test
    @Severity(SeverityLevel.TRIVIAL)
    void trivialDemo(){
        System.out.println("TRIVIAL");
    }
}
在这里插入图片描述
  • feature和sotry
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import org.junit.jupiter.api.Test;

/**
 * @Feature:大类
 * @Story:大类下面的小类
 */
@Feature("登录")
public class AllureFeatureTest {
    @Test
    @Story("登录成功")
    void loginSuccess(){
        System.out.println("登录成功");
    }
    @Test
    @Story("登录失败")
    void testFail1(){
        System.out.println("fail1");
    }
    @Test
    @Story("登录失败")
    void testFail2(){
        System.out.println("fail2");
    }
}

在首页会做具体展示,点进去可查看详情


在这里插入图片描述
  • 添加测试步骤
import io.qameta.allure.Allure;
import io.qameta.allure.Step;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

public class AllureStepTest {
    @Test
    public void stepTest() {
        Allure.step("step1", this::step1);
        Allure.step("step2", this::step2);
        Allure.step("step2", this::step3);
    }

    @Step("step1")
    void step1() {
        System.out.println("step1");
    }

    @Step("step2")
    void step2() {
        System.out.println("step2");
    }

    @Step("step3")
    void step3() {
        Assert.assertEquals(1, 2);
    }
}

在页面展示如下:如果成功会显示√标识,失败会标红并且显示报错


在这里插入图片描述
  • 添加附件:支持添加文本/图片/视频等
import io.qameta.allure.Allure;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class AllureAddAttachmentTest {
    @Test
    void addTextAndPics(){
        Allure.addAttachment("添加文本","我是文本内容");
        try {
            Allure.addAttachment("添加图片","image/png", new FileInputStream("C:\\Users\\86189\\Pictures\\clipboard1.png"),"png");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

测试报告中文展示乱码问题解决

在这里插入图片描述

在pom文件添加properties配置文件

 <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <aspectj.version>1.8.10</aspectj.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

我这里也改了,但是我感觉改不改关系不大,主要还是properties配置文件生效


在这里插入图片描述
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容