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>
为什么不建立自己的'@ BeforeClass'标注的方法内的逻辑看起来在一个共享的布尔检查' init()'在调用之前已经调用过了? TestNG AFAIK不提供任何可满足此需求的开箱即用功能。 –
但并行执行不会同时调用@BeforeClass吗?在这种情况下,多个线程都将在同一个实例中调用init方法? – vikramvi
并行执行是针对'@ Test'方法IMO而不是针对'@ BeforeClass'。当你尝试时会发生什么?你有一个可以显示问题的样本测试吗? –