[英]How to set invocation count during execution for the method using TestNG & Java
I am having one test method, which I am manually executing for 200 times. 我有一种测试方法,我手动执行了200次。
@Test(priority=2, invocationCount = 200)
public void inviteTalents() throws InterruptedException
{
logger.log(Status.INFO, "Count " + logins[count]);
}
How can I set the invocation count variable? 如何设置调用计数变量? I have tried something like this, but doesn't work.
我已经尝试过类似的方法,但是没有用。 Any help?
有什么帮助吗?
@BeforeMethod
public void setUp(Method method, ITestContext context) {
if(method.getName().equals("test3"))
{
ITestNGMethod currentTestNGMethod = null;
for (ITestNGMethod testNGMethod : context.getAllTestMethods())
{
if (testNGMethod.getInstance() == this)
{
currentTestNGMethod = testNGMethod;
break;
}
}
currentTestNGMethod.setInvocationCount(count);
}
}
You can make use an IAnnotationTransformer
implementation to get this done. 您可以使用
IAnnotationTransformer
实现来完成此操作。
Here's a sample that shows how to pass in the method name and the invocation count via JVM arguments and how the annotation transformer implementation changes the invocation count in runtime. 这是一个示例,该示例显示如何通过JVM参数传递方法名称和调用计数,以及注释转换器实现如何在运行时更改调用计数。
package com.rationaleemotions.stackoverflow.qn51160440;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Test;
public class TestClassSample {
@Test
public void fooTest() {
ITestResult r = Reporter.getCurrentTestResult();
String methodname = r.getMethod().getMethodName();
System.err.println(
"Running " + methodname + "() on Thread [" + Thread.currentThread().getId() + "]");
}
}
Here's how the annotation transformer looks like 这是注释转换器的外观
package com.rationaleemotions.stackoverflow.qn51160440;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
public class AnnotationTransformerImpl implements IAnnotationTransformer {
@Override
public void transform(
ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
//Pass the value via JVM argument -Dkvp=someMethod=400
//Here "someMethod" is the name of the method and 400 is the invocation count value
String kvp = System.getProperty("kvp", "fooTest=200");
String keyValue[] = kvp.split("=");
if (keyValue.length != 2) {
return;
}
if (!testMethod.getName().equalsIgnoreCase(keyValue[0])) {
return;
}
annotation.setInvocationCount(Integer.parseInt(keyValue[1]));
annotation.setThreadPoolSize(25);
}
}
Here's how the suite file looks like 这是套件文件的外观
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="45160355_Suite" parallel="methods" verbose="2" >
<listeners>
<listener
class-name="com.rationaleemotions.stackoverflow.qn51160440.AnnotationTransformerImpl"/>
</listeners>
<test name="45160355_test" verbose="2">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn51160440.TestClassSample"/>
</classes>
</test>
</suite>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.