2016-12-05 112 views
0

我正在學習使用TestNg進行單元測試。我想要將變量「val」的唯一值傳遞給線程池中的每個線程,但是它不會提取它。與testng做多線程單元測試

這裏TestNG的類:

public class NewTest { 

    int val = 0; 

    /*@Test(dataProvider = "dp") 
    public void f(Integer n, String s) { 
    }*/ 
    @BeforeMethod 
    public void beforeMethod() { 
     long id = Thread.currentThread().getId(); 
     System.out.println("beforeMethod. Thread id is: " + id); 



    } 

    @AfterMethod 
    public void afterMethod() {/* 
     long id = Thread.currentThread().getId(); 
     System.out.println("After test-method. Thread id is: " + id);*/ 
    } 


    @DataProvider 
    public Object[][] dp() { 
    return new Object[][] { 
     new Object[] { 1, "a" }, 
     new Object[] { 2, "b" }, 
    }; 
    } 
    @BeforeClass 
    public void beforeClass() { 

    } 

    @AfterClass 
    public void afterClass() { 
    } 

    @BeforeTest 
    public void beforeTest() { 
     val++; 
    } 

    @AfterTest 
    public void afterTest() { 
    } 

    @BeforeSuite 
    public void beforeSuite() { 
    } 

    @AfterSuite 
    public void afterSuite() { 
    } 

    @Test(threadPoolSize = 5, invocationCount = 5, timeOut = 1000) 
    public void methodOne(){ 
     System.out.println("Value of val from MethodOne::"+val); 
    } 
} 

輸出:

[ThreadUtil]開始執行超時:1000毫秒工人:5 threadPoolSize:5 beforeMethod。線程ID是:15 beforeMethod。線程 id是:12 beforeMethod。線程ID是:14 beforeMethod。線程ID是: 13 beforeMethod。線程ID爲:16來自MethodOne :: 1的val的值值來自MethodOne :: 1的val的值 來自MethodOne :: 1的val的值來自MethodOne :: 1的val的值 的值來自MethodOne :: 1的val的值PASSED:methodOne 通過:methodOne通過:methodOne通過:methodOne通過: methodOne

================================ =============== 默認測試

試驗運行:5,故障:0,跳過:0

============ ===================================默認套件總測試運行:5,失敗:0,跳過:0

[TestNG的]時間採取[FailedReporter傳遞= 0失敗= 0跳過= 0]:1個 MS [TestNG的]採取 [email protected]8時間:50毫秒[TestNG的]時間 拍攝者[email protected]:7 ms [TestNG] [email protected]拍攝時間:9 ms [TestNG] org.testng.reporters.jq.Main拍攝的時間@ 1d16f93d:40毫秒 [TestNG的]拍攝時間 [email protected]:4毫秒

回答

0

小心:@BeforeTest不是@BeforeMethod@BeforeSuite@BeforeTest@BeforeClass@BeforeMethod@BeforeGroup哪一個具體一個)。

然後,@BeforeTest是呼叫只能由<test>一次:你的樣品中,只有一次,val始終爲1

+0

我@BeforeMethod改變增量的一步,但我仍得到相同的輸出。 – focode

+0

你能與updat更新您的問題編輯樣本? – juherr

0

您可以利用ThreadLocal如下圖所示:(但請記住,TestNG的保證,只有@BeforeMethod>@Test >@AfterMethod註解的方法會在同一線程上運行。

public class NewTest { 
    private static ThreadLocal<Long> val = new ThreadLocal<>(); 

    @BeforeMethod 
    public void beforeMethod() { 
     long id = Thread.currentThread().getId(); 
     val.set(id); 
     printer("beforeMethod"); 
    } 

    @AfterMethod 
    public void afterMethod() { 
     printer("afterMethod"); 
     //usage of thread locale done. Lets reset it. 
     val.set(null); 
    } 

    @Test (threadPoolSize = 5, invocationCount = 5, timeOut = 1000) 
    public void methodOne() { 
     printer("methodOne"); 
    } 

    private static void printer(String prefix) { 
     System.err.println("Thread ID in the method " + prefix + "() is " + val.get()); 
    } 
}