2017-07-28 69 views
0

我用testng並行測試用例執行設置,但我只需要執行一次setup方法。需要在並行執行模式下只執行一次setup方法

BeforeClass,BeforeMethod也得到執行的單個線程。 但我需要在所有線程之前執行一次方法。

如何使用TestNG設置來實現此目的?

package com.howtodoinjava.parallelism; 

import org.testng.annotations.AfterClass; 
import org.testng.annotations.AfterTest; 
import org.testng.annotations.BeforeClass; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Parameters; 
import org.testng.annotations.Test; 

public class ParallelSuiteTest 
{ 
    String testName = ""; 

    @BeforeTest 
    @Parameters({ "test-name" }) 
    public void beforeTest(String testName) { 
     this.testName = testName; 
     long id = Thread.currentThread().getId(); 
     System.out.println("Before test " + testName + ". Thread id is: " + id); 
    } 

    @BeforeClass 
    public void beforeClass() { 
     long id = Thread.currentThread().getId(); 
     System.out.println("Before test-class " + testName + ". Thread id is: " 
       + id); 
    } 

    @Test 
    public void testMethodOne() { 
     long id = Thread.currentThread().getId(); 
     System.out.println("Sample test-method " + testName 
       + ". Thread id is: " + id); 
    } 

    @AfterClass 
    public void afterClass() { 
     long id = Thread.currentThread().getId(); 
     System.out.println("After test-method " + testName 
       + ". Thread id is: " + id); 
    } 

    @AfterTest 
    public void afterTest() { 
     long id = Thread.currentThread().getId(); 
     System.out.println("After test " + testName + ". Thread id is: " + id); 
    } 
} 

的testng.xml

<suite name="Test-class Suite" parallel="tests" thread-count="2"> 
    <test name="Test-class test 1"> 
     <parameter name="test-name" value="test-method One" /> 
     <classes> 
      <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" /> 
     </classes> 
    </test> 
    <test name="Test-class test 2"> 
     <parameter name="test-name" value="test-method One" /> 
     <classes> 
      <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" /> 
     </classes> 
    </test> 
</suite> 
+0

爲什麼不建立自己的'@ BeforeClass'標註的方法內的邏輯看起來在一個共享的布爾檢查' init()'在調用之前已經調用過了? TestNG AFAIK不提供任何可滿足此需求的開箱即用功能。 –

+0

但並行執行不會同時調用@BeforeClass嗎?在這種情況下,多個線程都將在同一個實例中調用init方法? – vikramvi

+0

並行執行是針對'@ Test'方法IMO而不是針對'@ BeforeClass'。當你嘗試時會發生什麼?你有一個可以顯示問題的樣本測試嗎? –

回答

1

下面的示例應該解釋什麼,我建議。

package com.rationaleemotions.stackoverflow.qn45371087; 

import org.testng.annotations.BeforeClass; 
import org.testng.annotations.Test; 

public class ParallelSuiteTest { 
    private static final Object lock = new Object(); 
    private static boolean initialised = false; 

    @BeforeClass 
    public void beforeClass() { 
     synchronized (lock) { 
      if (!initialised) { 
       init(); 
       initialised = true; 
      } 
     } 
    } 

    private void init() { 
     System.err.println("Initialisation done"); 
    } 

    @Test 
    public void testMethodOne() { 
     System.err.println("This is a test method running on [" + Thread.currentThread().getId() + "]"); 
    } 

} 

套房xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="45371087_Suite" verbose="2" parallel="tests" thread-count="10"> 
    <test name="45371087_Tests_1"> 
     <classes> 
      <class name="com.rationaleemotions.stackoverflow.qn45371087.ParallelSuiteTest"/> 
     </classes> 
    </test> 
    <test name="45371087_Tests_2"> 
     <classes> 
      <class name="com.rationaleemotions.stackoverflow.qn45371087.ParallelSuiteTest"/> 
     </classes> 
    </test> 
</suite> 

下面是輸出:

... 
... TestNG 6.12 by Cédric Beust ([email protected]) 
... 
Initialisation done 
This is a test method running on [12] 
This is a test method running on [11] 

=============================================== 
45371087_Suite 
Total tests run: 2, Failures: 0, Skips: 0 
===============================================