2016-12-05 5 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の値 from MethodOne :: 1 MethodOne :: 1からのvalの値 from MethodOne :: 1 MethodOne :: 1からのvalの値PASSED:methodOne methodOne PASSED:methodOne PASSED: PASSED:PASSEDはmethodOne methodOne

を================================ =============== デフォルトのテスト

テストを実行:5、失敗:0を、スキップ:0

============ =======================================================================================================================================================================================================================================================================================================================================

[TestNGの】時間で撮影した[FailedReporter = 0 = 0はスキップ失敗渡さ= 0]:1つの MSが[TestNGの】時間 [email protected]によって撮影された:50のMS [TestNGの】時間 を[email protected]:7 ms [TestNG]時間:[email protected]:9 ms [TestNG]時間:org.testng.reporters.jq.Main @ 1d16f93d:40ミリ秒は [TestNGの] [email protected]要した時間:4は、MS

答えて

0

は注意してください:@BeforeTest@BeforeMethod@BeforeSuite - >@BeforeTest - >@BeforeClass - >@BeforeMethodであり、特定のものは@BeforeGroupではありません)。

その後、@BeforeTest<test>で一度だけ呼び出しです:下記のようにあなたがThreadLocalを活用することができ、あなたのサンプルでは、​​一度だけとval常に1

+0

私は@BeforeMethodに増分ステップを変更しましたが、まだ私は同じ出力を取得しています。 – focode

+0

あなたがUPDATであなたの質問を更新でしたサンプル? – juherr

0

次のとおりです。(しかし、TestNGのは>@Testのみ@BeforeMethodを保証することを忘れないでください>@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()); 
    } 
} 
関連する問題