2015-05-07 37 views
0

之間的區別當我運行在神火測試,就是神火:suitesAndClasses和類

<parallel>suitesAndClasses</parallel> 

<parallel>classes</parallel> 

第一個例子,似乎在並行運行我的測試類之間的區別,而第二個則沒有。

回答

1

請在下面找到一個簡單的例子來驗證<parallel>classes</parallel>是否按預期工作。對<parallel>suitesAndClasses</parallel>的檢查,它將並行執行JUnit測試套件和正常的JUnit測試類。

的測試類

public class ParallelOneTest { 

    @Test 
    public void testOne() throws InterruptedException { 
     System.out.println("ParallelOneTest.testOne()"); 
     TimeUnit.SECONDS.sleep(5); 
    } 

    @Test 
    public void testTwo() throws InterruptedException { 
     System.out.println("ParallelOneTest.testTwo()"); 
     TimeUnit.SECONDS.sleep(5); 
    } 

    @Test 
    public void testThree() throws InterruptedException { 
     System.out.println("ParallelOneTest.testThree()"); 
     TimeUnit.SECONDS.sleep(5); 
    } 
} 

public class ParallelTwoTest { 

    @Test 
    public void testOne() throws InterruptedException { 
     System.out.println("ParallelTwoTest.testOne()"); 
     TimeUnit.SECONDS.sleep(5); 
    } 

    @Test 
    public void testTwo() throws InterruptedException { 
     System.out.println("ParallelTwoTest.testTwo()"); 
     TimeUnit.SECONDS.sleep(5); 
    } 

    @Test 
    public void testThree() throws InterruptedException { 
     System.out.println("ParallelTwoTest.testThree()"); 
     TimeUnit.SECONDS.sleep(5); 
    } 
} 

所有以下描述的試驗方案作爲mvn clean compile test

正常順序執行被執行

或者該文件pom.xml或至少沒有<parallel>...</parallel>子句爲它在沒有maven-surefire-plugin的配置。

順序輸出(剝離)

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.07 sec 
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.008 sec 
... 
[INFO] ------------------- 
[INFO] BUILD SUCCESS  
[INFO] ------------------- 
[INFO] Total time: 31.919s 

並行執行

的pom.xml

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.18</version> 
      <configuration> 
       <parallel>classes</parallel> 
       <threadCount>2</threadCount> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

並行輸出(剝離)

[INFO] parallel='classes', perCoreThreadCount=true, threadCount=2, ... 
... 
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.023 sec - in sub.optimal.mavenscratch.ParallelTwoTest 
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.023 sec - in sub.optimal.mavenscratch.ParallelOneTest 
... 
[INFO] ------------------- 
[INFO] BUILD SUCCESS  
[INFO] ------------------- 
[INFO] Total time: 16.770s 

正如您在第二個示例(基於總時間)中看到的,這兩個測試類都是並行執行的。

對於並行選項suitesAndClasses,可以配置許多類<threadCountClasses>和測試套件threadCountSuites可以並行執行。

+0

你好嗎所有正常工作?我想通過該方法運行一些測試,以及其他需要與該類一起運行的測試。 http://stackoverflow.com/questions/41387080/testng-parallel-classesandmethods-without-xml – Will

+1

@看起來好像在你的鏈接SO問題中已經回答了這個問題。 – SubOptimal