如何使用 TestNG & Java 在方法执行期间设置调用计数
How to set invocation count during execution for the method using TestNG & Java
我有一个测试方法,我手动执行了200次。
@Test(priority=2, invocationCount = 200)
public void inviteTalents() throws InterruptedException
{
logger.log(Status.INFO, "Count " + logins[count]);
}
如何设置调用计数变量?我试过这样的东西,但没有用。有帮助吗?
@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);
}
}
您可以使用 IAnnotationTransformer
实现来完成此操作。
下面的示例展示了如何通过 JVM 参数传入方法名称和调用计数,以及注释转换器实现如何在运行时更改调用计数。
package com.rationaleemotions.Whosebug.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() + "]");
}
}
这是注释转换器的样子
package com.rationaleemotions.Whosebug.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);
}
}
套件文件如下所示
<?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.Whosebug.qn51160440.AnnotationTransformerImpl"/>
</listeners>
<test name="45160355_test" verbose="2">
<classes>
<class name="com.rationaleemotions.Whosebug.qn51160440.TestClassSample"/>
</classes>
</test>
</suite>
我有一个测试方法,我手动执行了200次。
@Test(priority=2, invocationCount = 200)
public void inviteTalents() throws InterruptedException
{
logger.log(Status.INFO, "Count " + logins[count]);
}
如何设置调用计数变量?我试过这样的东西,但没有用。有帮助吗?
@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);
}
}
您可以使用 IAnnotationTransformer
实现来完成此操作。
下面的示例展示了如何通过 JVM 参数传入方法名称和调用计数,以及注释转换器实现如何在运行时更改调用计数。
package com.rationaleemotions.Whosebug.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() + "]");
}
}
这是注释转换器的样子
package com.rationaleemotions.Whosebug.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);
}
}
套件文件如下所示
<?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.Whosebug.qn51160440.AnnotationTransformerImpl"/>
</listeners>
<test name="45160355_test" verbose="2">
<classes>
<class name="com.rationaleemotions.Whosebug.qn51160440.TestClassSample"/>
</classes>
</test>
</suite>