使用注解的方式對TestNg線程池配置、執行次數配置、超時配置
注:使用注解來控制測試方法運行的次數和超時時間,timeOut在單線程或者多線程模式下都可用,threadPoolSize設置了線程池的個數 * ,在觀察結果時,發現很多值是重復的,但是可能不等於我們配置的線程池個數,因為線程的個數還取決於硬件CPU的支持,
invocationCount----表示執行的次數
threadPoolSize-----表示線程池的內線程的個數
timeOut-------超時時間-毫秒
代碼實例:
public class TestngInvocationCount { private static int sum = 0; @Test(threadPoolSize = 2, invocationCount = 10, timeOut = 1000) public void testServer() throws InterruptedException { // 檢測啟動的線程數,當啟動的個數超過CPU核數時,其實是重新在調度 // Thread.sleep(2000); sum++; System.out.println("........." + sum); } }
測試結果:
[TestNG] Running: C:\Users\Administrator\.IntelliJIdea2018.2\system\temp-testng-customsuite.xml .........1 .........2 .........3 .........4 .........5 .........6 .........7 .........8 .........9 .........10 =============================================== Default Suite Total tests run: 10, Failures: 0, Skips: 0 =============================================== Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 Process finished with exit code 0