testng集成allure测试报告

** pom.xml文件里面的配置 **

<parent>
    <groupId>ru.yandex.qatools.allure</groupId>
    <artifactId>allure-examples-parent</artifactId>
    <version>1.0</version>
</parent>

<properties>
    <selenide.version>4.4.1</selenide.version>
    <testng>fvt</testng>
    <aspectj.version>1.8.9</aspectj.version>
    <allure.version>1.5.2</allure.version>
</properties>


<!-- testng-->
<dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.9.10</version>
</dependency>
<!-- selenide-->
<dependency>
      <groupId>com.codeborne</groupId>
      <artifactId>selenide</artifactId>
      <version>${selenide.version}</version>
      <scope>test</scope>
</dependency>
<!-- Allure -->
<dependency>
    <groupId>ru.yandex.qatools.allure</groupId>
    <artifactId>allure-testng-adaptor</artifactId>
    <version>${allure.version}</version>
    <exclusions>
        <exclusion>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- allure-->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
                <suiteXmlFiles>
                    <suiteXmlFile>${testng}.xml</suiteXmlFile>
                </suiteXmlFiles>
                <argLine>
                    -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                </argLine>
                <systemProperties>
                    <property>
                        <name>allure.results.directory</name>
                        <value>${project.build.directory}/allure-results</value>
                    </property>
                </systemProperties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>

        <!--Needed only to show reports locally. Run jetty:run and
        open localhost:8080 to show the report-->
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.2.10.v20150310</version>
            <configuration>
                <webAppSourceDirectory>${project.build.directory}/site/allure-maven-plugin</webAppSourceDirectory>
                <stopKey>stop</stopKey>
                <stopPort>1234</stopPort>
            </configuration>
        </plugin>
    </plugins>
</build>
<reporting>
    <excludeDefaults>true</excludeDefaults>
    <plugins>
        <plugin>
            <groupId>ru.yandex.qatools.allure</groupId>
            <artifactId>allure-maven-plugin</artifactId>
            <version>2.5</version>
        </plugin>
    </plugins>
</reporting>

上传错误case的截图?
实现一个监听器
创建一个类AllureReporterListener实现IHookable接口

public class AllureReporterListener implements IHookable{

    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        callBack.runTestMethod(testResult);
        if (testResult.getThrowable() != null) {
            try {
                takeScreenShot(testResult.getMethod().getMethodName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } 

    @Attachment(value = "Failure in method {0}", type = "image/png") 
    private byte[] takeScreenShot(String methodName) throws Exception{
        File screenshot = Screenshots.getLastScreenshot();
        return Files.toByteArray(screenshot);
    }
}

如何使用该监听器?
两种方法:

  1. 在testng.xml中加入一个listeners标签
<suite name="TestAll" parallel="methods" skipfailedinvocationcounts="true" thread-count="1">
    <listeners>
        <listener class-name="com.alibaba.selenide.utils.AllureReporterListener"/>
    </listeners>
    <test name="AllTest">
        <classes>
            <class name="com.alibaba.selenide.cases.IdeTest" />
        </classes>
    </test>
</suite>
  1. 在case中加入@Listeners注解
@Listeners
public class IdeTest extends BaseTest{

    @BeforeMethod
    public void setup() throws Exception {
        preprocess();
    }

    @Title("创建节点,运行,提交,测试运行")
    @Test
    public void nodeTest() {
          
    }

** 如何运行? **

先运行: mvn clean test 运行case
再运行: mvn site 生成站点
最后运行: mvn jetty:run 查看报告

** 可能遇到的问题? **

  1. 生成的报告每个case出现两个一模一样的case
    https://github.com/allure-framework/allure1/issues/731
    去掉pom文件中maven-surefire-plugin中这个配置
<properties>
      <property>
            <name>listener</name>
           <value>ru.yandex.qatools.allure.testng.AllureTestListener</value>
      </property>
</properties>
展开阅读全文

没有更多推荐了,返回首页