我正在尝试使用ITest接口来获取来自数据提供者的测试的自定义名称(对于Jenkins报告)。我尝试了一个如下所示的示例程序,以了解TestNG调用gettestname()方法的几点。
粘贴输出和程序。
正如您在下面看到的那样,输出令人困惑,并且测试名称也会出现'null'。所以我的问题是Itest是否设计用于数据提供者?此外,当我们使用parallel = true运行下面的数据提供者时,由于成员'testName'在线程之间共享并且只有一个类实例正在运行,因此它变得更加混乱。
我可以看到ITest如何与@factory一起使用,因为当我们使用工厂时,每个testmethod运行都会调用一个新的TestClass,所以那里不会有问题。
输出:
TestNGThreadingTest:31 - DataProvider running on Thread id = 1
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = null
TestNGThreadingTest:43 - Before Method Thread id = 1 TestName = test1
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test1
TestNGThreadingTest:37 - Run Method Thread id = 1 TestName = test1
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test1
TestNGThreadingTest:48 - After Method Thread id = 1 TestName = test1
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test1
TestNGThreadingTest:43 - Before Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:37 - Run Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:48 - After Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:43 - Before Method Thread id = 1 TestName = test3
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test3
TestNGThreadingTest:37 - Run Method Thread id = 1 TestName = test3
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test3
TestNGThreadingTest:48 - After Method Thread id = 1 TestName = test3
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test3
TestNGThreadingTest:43 - Before Method Thread id = 1 TestName = test4
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test4
TestNGThreadingTest:37 - Run Method Thread id = 1 TestName = test4
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test4
TestNGThreadingTest:48 - After Method Thread id = 1 TestName = test4
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test4
TestNGThreadingTest:43 - Before Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:37 - Run Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:48 - After Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test5
PASSED: test1("test1")
PASSED: test2("test2")
PASSED: test3("test3")
PASSED: test4("test4")
PASSED: test5("test5")
输出[当parallel = true]
TestNGThreadingTest:31 - DataProvider running on Thread id = 1
TestNGThreadingTest:53 - GetTestname Method Thread id = 9 TestName = null
TestNGThreadingTest:53 - GetTestname Method Thread id = 11 TestName = null
TestNGThreadingTest:53 - GetTestname Method Thread id = 10 TestName = null
TestNGThreadingTest:53 - GetTestname Method Thread id = 13 TestName = null
TestNGThreadingTest:53 - GetTestname Method Thread id = 12 TestName = null
TestNGThreadingTest:43 - Before Method Thread id = 11 TestName = test3
TestNGThreadingTest:43 - Before Method Thread id = 13 TestName = test5
TestNGThreadingTest:43 - Before Method Thread id = 9 TestName = test1
TestNGThreadingTest:43 - Before Method Thread id = 12 TestName = test4
TestNGThreadingTest:43 - Before Method Thread id = 10 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 12 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 9 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 13 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 11 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 10 TestName = test2
TestNGThreadingTest:37 - Run Method Thread id = 11 TestName = test3
TestNGThreadingTest:37 - Run Method Thread id = 12 TestName = test4
TestNGThreadingTest:37 - Run Method Thread id = 9 TestName = test1
TestNGThreadingTest:37 - Run Method Thread id = 10 TestName = test2
TestNGThreadingTest:37 - Run Method Thread id = 13 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 12 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 11 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 9 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 10 TestName = test2
TestNGThreadingTest:48 - After Method Thread id = 12 TestName = test4
TestNGThreadingTest:48 - After Method Thread id = 10 TestName = test2
TestNGThreadingTest:48 - After Method Thread id = 9 TestName = test1
TestNGThreadingTest:53 - GetTestname Method Thread id = 13 TestName = test2
TestNGThreadingTest:48 - After Method Thread id = 11 TestName = test3
TestNGThreadingTest:48 - After Method Thread id = 13 TestName = test5
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
TestNGThreadingTest:53 - GetTestname Method Thread id = 1 TestName = test2
PASSED: test2("test3")
PASSED: test2("test4")
PASSED: test2("test1")
PASSED: test2("test2")
PASSED: test2("test5")
代码:
public class TestNGThreadingTest implements ITest {
public String testName;
final static Logger logger = Logger.getLogger(TestNGThreadingTest.class);
@DataProvider(name="dp")
public Iterator<Object[]> getTests() {
Collection<Object[]> tests = new ArrayList<Object[]>();
tests.add(new Object[]{"test1"});
tests.add(new Object[]{"test2"});
tests.add(new Object[]{"test3"});
tests.add(new Object[]{"test4"});
tests.add(new Object[]{"test5"});
logger.info("DataProvider running on Thread id = " + Thread.currentThread().getId());
return tests.iterator();
}
@Test(dataProvider="dp")
public void run(String testName) {
logger.info("Run Method Thread id = " + Thread.currentThread().getId() + " TestName = "+ testName );
}
@BeforeMethod
public void init(Object[] testArgs) {
this.testName = (String) testArgs[0];
logger.info("Before Method Thread id = " + Thread.currentThread().getId() + " TestName = " + testArgs[0]);
}
@AfterMethod
public void tearDown(Object[] testArgs) {
logger.info("After Method Thread id = " + Thread.currentThread().getId() + " TestName = " + testArgs[0]);
}
@Override
public String getTestName() {
logger.info("GetTestname Method Thread id = " + Thread.currentThread().getId() + " TestName = " + testName);
return testName;
}
}
答案 0 :(得分:0)
这是一个确定性问题(可能是TestNG问题吗?)。
按String testName
重新ThreadLocal<String> testName = new ThreadLocal()
可以解决您的问题。
或者您也可以使用与@Factory
相关联的@DataProvider
,但@Factory
may not work in parallel。