我有一个长时间运行的 @test 注释方法,我需要重复运行它,我使用 invocationCount
实现了这一点。
当我对值进行硬编码时,例如invocationCount=10
然后它成功运行,依次运行测试 1 - 10,即它等待方法完成,然后开始下一次调用。
但是,我需要能够在运行时更改 invocationCount
值,因此实现了 IAnnotationTransformer
接口来实现这一点,在运行时传入键值对。但是,这具有并行运行测试的效果。我在这里提供了一个简化版本:
TestNG.xml:
<suite name="Run tests sequentially" verbose="1" thread-count="1" configfailurepolicy="continue">
<listeners>
<listener
class-name="com.tests.utils.AnnotationTransformerImpl"/>
</listeners>
<test name="invocation-test-synchronised" parallel="false">
<classes>
<class name="com.tests.journeys.InvocationTest"/>
</classes>
</test>
</suite>
注解转换器: 公共类 AnnotationTransformerImpl 实现了 IAnnotationTransformer {
private String invocation_key = System.getProperty("invKey");
private String invocation_count = System.getProperty("invCount");
//value passed from JVM is -Dkvp=invocationTest=5
@Override
public void transform(
ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
String kvp = System.getProperty(invocation_key,invocation_count);
String keyValue[] = kvp.split("=");
if (keyValue.length != 2) {
return;
}
if (!testMethod.getName().equalsIgnoreCase(keyValue[0])) {
return;
}
annotation.setInvocationCount(Integer.parseInt(keyValue[1]));
annotation.setThreadPoolSize(5);
}
}
测试类: 公共类 InvocationTest {
@Test(invocationCount = 5)
public void invocationTest() {
ITestResult r = Reporter.getCurrentTestResult();
String methodname = r.getMethod().getMethodName();
System.err.println(
"Running " + methodname + "() on Thread [" + Thread.currentThread().getId() + "]");
Thread.sleep(5000);
}
}
生成的测试输出在单独的线程上并行运行测试方法:
如何在开始下一次调用之前将方法运行到完成?我知道它会在某个地方的文档中,只是在挣扎。
答案 0 :(得分:0)
尝试谷歌:Java 多线程。
它会给你一个解决方案。可能使用 Thread.join();
和其他东西。