Maven与Ant使用reportNG代替testng

大家在使用TestNG时一定会发现其本身的报告生成不但简陋而且相当的不美观,而ReportNG正是一款为TestNG量身定做的报告生成插件,其报告美观、简约、清晰、漂亮的特点让很多TestNG开始慢慢放弃了其默认生成的结果报告。

那么就开始讲解如何使用maven配置reportNG。只需在pom.xml中加入以下配置:

<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>iquicktest.com</groupId>
  <artifactId>selenium_maven_eclipse</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>selenium_maven_eclipse</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.1.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.uncommons</groupId>
      <artifactId>reportng</artifactId>
      <version>1.1.2</version>
      <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>3.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.35.0</version>
    </dependency>
  </dependencies>
 
  <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <properties>
                        <property>
                            <name>usedefaultlisteners</name>
                            <value>false</value>
                        </property>
                        <property>
                            <name>listener</name>
                            <value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</value>
                        </property>
                    </properties>
                    <workingDirectory>target/</workingDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这里要将defaultListener设置为false,下面配置了两个listener,一个是HTMLReport,用来生成HTML格式的Report,别一个是JUnitXMLReporter,这个是用来生成xml格式的report,用于jekins服务器

运行mvn test,之后在targetsurefire-reportshtml查看结果。

最终显示结果:

再来讲解下Ant中build.xml

<!--指定testNg需要的Jar包-->  
<taskdef resource="testngtasks" classpath="${lib.dir}/testng-6.2.jar"/>  
  
<target name="run_tests" depends="compile" description="执行TestNg测试用例">  
    <testng classpathref="compile.path"   
        outputDir="${output.dir}"   
        haltOnfailure="true"   
        useDefaultListeners="false"   
        listeners="org.uncommons.reportng.HTMLReporter,org.testng.reporters.FailedReporter" >  
        <!--设置TestNg所包含的xml文件-->  
        <xmlfileset dir="${basedir}" includes="testng.xml" />  
        <!--设置报告Title名称 -->  
        <sysproperty key="org.uncommons.reportng.title" value="自动化测试报告" />  
    </testng>  
</target>  

useDefaultListeners = "false" 用来禁止TestNg产生报告,但是我们还需要他的错误报告testng-fails.xml文件,为了方便我们只关注未通过的测试,所以还要将TestNg的org.testng.reporters.FailedReporter监听器加上。

 注:org.uncommons.reportng.HTMLReporter为reportNg的报告监听器

原文地址:https://www.cnblogs.com/yangxia-test/p/4428901.html