编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Allure+TestNG打造完美的自动化测试报告

wxchong 2024-10-05 02:20:55 开源技术 12 ℃ 0 评论

引言

上一节分享Extent Reports,这次分享一个与之齐名的Allure。本篇只是翻译国外一大神的文章。这位大神前一篇也分享了Extent Reports,这里只翻译第二篇。所以你的看到代码里包括两个报告框架。Allure是Yandex QA Team团队开发的。



依赖

想要集成Allure,需要在pom.xml里添加以下依赖。

Properties Section

<properties>
    <aspectj.version>1.8.10</aspectj.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Allure TestNG Dependency

<dependency>
     <groupId>io.qameta.allure</groupId>
     <artifactId>allure-testng</artifactId>
     <version>2.12.0</version>
</dependency>

Build Section

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>TestNG.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

搞定,这就是所有pom.xml里要添加的东西。最终的pom是这个样子:(TestNG建议改成最新版本,不然会报错。)


?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>allurereportexample</groupId>
    <artifactId>allurereportexample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <aspectj.version>1.8.13</aspectj.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
        </dependency>

        <dependency>
            <groupId>ru.yandex.qatools.ashot</groupId>
            <artifactId>ashot</artifactId>
            <version>1.5.4</version>
        </dependency>

        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>2.12.0</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>2.41.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>TestNG.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

配置Allure Features并修改测试代码

接下来我会基于下面的图片,逐个讲解每个Feature。我对测试代码做了一些修改,让生成的HTML报告更易懂,展示更多有用信息。


Feature-1: 显示名称

为了让测试报告更容易理解, 这里用了@test注解的description属性。这样通常是用例的主题。


@Test (priority = 0, description=”Invalid Login Scenario with wrong username and password.”)

然后,你也可以用 @Description 注解添加更多描述信息:


@Description(“Test Description: Login test with wrong username and wrong password.”)

在测试报告中会展示成如下这个样子:

Feature-2: Steps

Step可以理解为测试步骤,它们可以被任何测试用例调用,所以在项目中要放一个公共的地方。用@Step 注解标示它是一个测试步骤。在示例项目中,我们把Step统一放在Page中。 LogintoN11, verifyLoginUserName, verifyLoginPassword都是Step。你可以看到下面的示例代码的方法上方都加了@Step注解。我们还可以用{}传参。比如:第一个方法,{0} 是第一个参数 – username, {1} 是第二个参数 – password.

LoginPage Steps:

HomePage Steps:

在测试报告中的样子如下:

Feature-3: Attachments

我们可以用@Attachment注解在报告中添加附件。该方法可以返回String, byte []等。比如:我们可以在测试报告中添加截图,该方法要返回byte []。别忘记了,在类上面加上@Listeners({ TestListener.class })声明。

在TestListener类中添加了两个附件,一个是txt,另外一个是截图。

当用例失败时就会调用这两个方法。

在测试报告中会是以下这个样子:

Feature-4: Links

你可以用@Link注解集成缺陷管理系统或者用例管理系统。

import io.qameta.allure.Link;
import io.qameta.allure.Issue;
import io.qameta.allure.TmsLink;

@Link("https://example.org")
@Link(name = "allure", type = "mylink")
public void testSomething() {
     ...
}

@Issue("123")
@Issue("432")
public void testSomething() {
     ...
}

@TmsLink("test-1")
@TmsLink("test-2")
public void testSomething() {
     ...
}

property文件中配置的格式如下:allure.link.my-link-type.pattern=https://example.org/custom/{}/path, allure会自动用注释上的值替换{}。

allure.link.mylink.pattern=https://example.org/mylink/{}
allure.link.issue.pattern=https://example.org/issue/{}
allure.link.tms.pattern=https://example.org/tms/{}

在示例项目中没有使用link这个功能,所以测试报告中没有。

Feature-5: Severity

我们可以用@Severity注解指定严重级别。

测试报告中是这个样子:


Feature-6: BDD报告 (Features and Stories)

喜欢BDD或者敏捷的同学,可以使用@Epic, @Feature和@Stories注解,话不多说看了代码和报告就明白了。

报告中的样子:

先介绍到这里,接下来我们执行一下,看下报告。

代码:https://github.com/swtestacademy/TestNGAllureReport

Step-3: 执行,生成Allure报告

你可以用Maven命令(mvn clean test)或者直接在IDEA执行。

IDEA中点击configurations

选择maven,在红色箭头处输入clean test,点OK。

点绿色的图标执行。

是时候生成测试报告了。

为了生成报告要安装Allure command-line解释器。

1.下载最新的zip包https://bintray.com/qameta/generic/allure2/2.7.0#files/io%2Fqameta%2Fallure%2Fallure%2F2.7.0

2. 点Files,下载zip包。zip是windows用的。如果你是mac的话可以用brew install 安装。

3.解压

4.打开bin目录

5.设置环境变量



Mac用户直接用下面命令:


brew install allure

最后,打开命令行工具,导航到项目目录,执行以上命令:


allure serve allure-results


然后,你将看到漂亮的Allure报告.

Dashboard

Categories


Suites

Graphs


Timeline

Behaviors


Packages

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表