maven surefire插件與testng


總結就是:參考文章:https://www.jianshu.com/p/2594dcbd3272

Maven surefire插件是一個執行maven項目測試代碼的插件。

Maven本身並不是一個單元測試框架,Java世界中主流的單元測試框架為JUnit和TestNG。Maven所做的只是在構建執行到特定生命周期階段的時候,通過插件來執行JUnit或者TestNG的測試用例。這一插件就是maven-surefire-plugin,可以稱之為測試運行器(Test Runner),他能很好的兼容JUnit 3、JUnit 4以及TestNG。

我們知道,生命周期階段需要綁定到某個插件的目標才能完成真正的工作,test階段正是maven-surefire-plugin的test目標相綁定了,這是一個內置的綁定。

1、maven-surefire-plugin插件默認會自動執行測試源碼包(即test目錄下)中遵循以下命名規則的java測試類。(有文章說如果測試method不是以test*開頭,就不會執行到該測試方法。應該指的是沒有做任何自定義configuration配置的默認情況下)

  1. **/Test*.java
  2. **/*Test.java
  3. **/*TestCase.java

對應的pom.xml

<build>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <version>2.19</version>

                <configuration>

                    <encoding>UTF-8</encoding>

                    <!--  <suiteXmlFiles>

                        <suiteXmlFile>TestNG.xml</suiteXmlFile>

                    </suiteXmlFiles>-->

                    <test>Order*.java</test><!-- test命令默認執行test目錄下的文件 -->

                    <systemPropertyVariables>

                        <ddg.sys.name>htl_hse</ddg.sys.name>

                        <!-- <maven.surefire.debug>true</maven.surefire.debug> -->

                    </systemPropertyVariables>

                </configuration>

            </plugin>

</plugins>

</build>

 

2、也可以通過指定<suiteXmlFiles>屬性來運行t指定的testng.xml執行測試套

(將< suiteXmlFile>的value值設置為引用properties,則更靈活。執行命令時就可以指定想這些的testng.xml eg:mvn clean test -DsuiteXmlFile=src/test/resources/xml/a.xml)

對應的pom.xml

<properties>

        <suiteXmlFile>testng.xml</suiteXmlFile>

    </properties>

<plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <version>2.19</version>

                <configuration>

                    <encoding>UTF-8</encoding>

                    <suiteXmlFiles>

                        <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>

                    </suiteXmlFiles>

                </configuration>

            </plugin>

</plugins>

 

 

3、更多surefire plugin的使用方法可以通過maven-help-plugin插件查看當前測試插件綁定的生命周期
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-surefire-plugin:2.7 -Ddetail

mvn surefire:help -Ddetail=true -Dgoal=test


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM