大家在使用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,之后在target\surefire-reports\html查看結果。
最終顯示結果:
再來講解下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的報告監聽器