歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

surefire 和 surefire-report plugin

今天遇到了很奇怪的事情,浪費了兩個小時。還是因為過於自信,沒有仔細看配置。

maven 配置裡面怎麼也不能用上自己配置的testng.xml, 用了這麼久,居然遇到這種問題。為此特意寫了一個測試工程。

最後才發現,居然在改用surefire的地方用到了surefire-report,

下面是正確的配置,testng.xml就在項目根目錄下,

  1.  <build>  
  2.    <plugins>  
  3.      <plugin>  
  4. <groupId>org.apache.maven.plugins</groupId>  
  5. <artifactId>maven-surefire-plugin</artifactId>  
  6. <version>2.12</version>  
  7. <configuration>  
  8.   <suiteXmlFiles>  
  9.     <suiteXmlFile>testng.xml</suiteXmlFile>  
  10.   </suiteXmlFiles>  
  11. </configuration>  
  12.      </plugin>  
  13.    </plugins>  
  14.  </build>  

當然也可以放在src/test/resources目錄下,這樣的話要寫成

  1. <suritXmlFile>src/test/resources/testng.xml</suritXmlFile>  
testng.xml內容很簡單:
  1. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >  
  2. <suite name="ClientApi" verbose="1" >  
  3.     <test name="functionTest">  
  4.         <groups>  
  5.             <run>  
  6.                 <exclude name="g1"/>  
  7.             </run>  
  8.         </groups>  
  9.         <classes>  
  10.             <class name="com.mycompany.testngexample.AppTest"></class>  
  11.         </classes>  
  12.     </test>  
  13. </suite>  
如果不小心復制成了surefire-report-plugin, testng.xml會被忽略,然後所有的測試程序都會被執行。


我為什麼會犯這個錯誤,因為我的父工程的pom.xml裡面用到了site plugin,site plugin裡面用到了surefire-report-plugin。


實際上surefire plugin運行測試代碼,並將報告放在${basedir}/target/surefire-reports目錄下

文件類型是txt和xml

而surefire report plugin負責將xml文件轉換成html文件,這樣就可以和site產生的報告合在一起。


所以項目中應該兩個都需要,surefire-plugin單獨配置,而surefire-report-plugin用在site-plugin內部

如果只用surefire-report plugin,我們就不能控制那些UT需要運行,哪些不需要運行。

這件事情其實暴露了maven的一個問題,如果surefire-plugin不支持某些設置,比如testng的配置,就應該及時報錯,而不是悄無聲息的做一些默認行為。

友好一點,大家生活都輕松點。


另外,如果將surefire-plugin配置在parent pom中,指定的testng.xml不用任何路徑前綴的話,在child project中運行mvn test,就會找child project目錄下的testng.xml,互相不干擾。非常方便。

Copyright © Linux教程網 All Rights Reserved