如何在运行时在 TestNG - @Test 中设置 InvocationCount?
How do I set the InvocationCount at runtime in TestNG - @Test?
我想 运行 我的测试方法基于业务逻辑多次。有没有办法改变 InvocationCount 来达到同样的效果?也欢迎任何其他建议。
您基本上需要利用 IAnnotationTransformer 来执行此操作。
下面是一个演示此操作的示例。
我们将用来指示特定测试方法需要 运行 多次的标记注释。
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
/**
* A Marker annotation which is used to express the intent that a particular test method
* can be executed more than one times. The number of times that a test method should be
* iterated is governed by the JVM argument : <code>-Diteration.count</code>. The default value
* is <code>3</code>
*/
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface CanRunMultipleTimes {
}
测试 class 看起来像这样。
import org.testng.annotations.Test;
import java.util.concurrent.atomic.AtomicInteger;
public class TestClassSample {
private volatile AtomicInteger counter = new AtomicInteger(1);
@CanRunMultipleTimes
@Test
public void testMethod() {
System.err.println("Running iteration [" + counter.getAndIncrement() + "]");
}
}
这是注释转换器的样子。
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class SimpleAnnotationTransformer implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
if (testMethod == null || testMethod.getAnnotation(CanRunMultipleTimes.class) == null) {
return;
}
int counter = Integer.parseInt(System.getProperty("iteration.count", "3"));
annotation.setInvocationCount(counter);
}
}
套件 xml 文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="46998341_Suite" verbose="2">
<listeners>
<listener class-name="com.rationaleemotions.Whosebug.qn46998341.SimpleAnnotationTransformer"/>
</listeners>
<test name="46998341_Test">
<classes>
<class name="com.rationaleemotions.Whosebug.qn46998341.TestClassSample"/>
</classes>
</test>
</suite>
输出结果如下:
... TestNG 6.12 by Cédric Beust (cedric@beust.com)
...
Running iteration [1]
Running iteration [2]
Running iteration [3]
PASSED: testMethod
PASSED: testMethod
PASSED: testMethod
===============================================
46998341_Test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
46998341_Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
你能做到 class 水平吗?感谢您的帮助,方法级别工作完美,只是想知道我们是否可以为 Class 级别也做
我在下面尝试过,但没有用。使用类型
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD, TYPE})
public @interface CanRunMultipleTimes {
}
我想 运行 我的测试方法基于业务逻辑多次。有没有办法改变 InvocationCount 来达到同样的效果?也欢迎任何其他建议。
您基本上需要利用 IAnnotationTransformer 来执行此操作。
下面是一个演示此操作的示例。
我们将用来指示特定测试方法需要 运行 多次的标记注释。
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
/**
* A Marker annotation which is used to express the intent that a particular test method
* can be executed more than one times. The number of times that a test method should be
* iterated is governed by the JVM argument : <code>-Diteration.count</code>. The default value
* is <code>3</code>
*/
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface CanRunMultipleTimes {
}
测试 class 看起来像这样。
import org.testng.annotations.Test;
import java.util.concurrent.atomic.AtomicInteger;
public class TestClassSample {
private volatile AtomicInteger counter = new AtomicInteger(1);
@CanRunMultipleTimes
@Test
public void testMethod() {
System.err.println("Running iteration [" + counter.getAndIncrement() + "]");
}
}
这是注释转换器的样子。
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class SimpleAnnotationTransformer implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
if (testMethod == null || testMethod.getAnnotation(CanRunMultipleTimes.class) == null) {
return;
}
int counter = Integer.parseInt(System.getProperty("iteration.count", "3"));
annotation.setInvocationCount(counter);
}
}
套件 xml 文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="46998341_Suite" verbose="2">
<listeners>
<listener class-name="com.rationaleemotions.Whosebug.qn46998341.SimpleAnnotationTransformer"/>
</listeners>
<test name="46998341_Test">
<classes>
<class name="com.rationaleemotions.Whosebug.qn46998341.TestClassSample"/>
</classes>
</test>
</suite>
输出结果如下:
... TestNG 6.12 by Cédric Beust (cedric@beust.com)
...
Running iteration [1]
Running iteration [2]
Running iteration [3]
PASSED: testMethod
PASSED: testMethod
PASSED: testMethod
===============================================
46998341_Test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
46998341_Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
你能做到 class 水平吗?感谢您的帮助,方法级别工作完美,只是想知道我们是否可以为 Class 级别也做
我在下面尝试过,但没有用。使用类型
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD, TYPE})
public @interface CanRunMultipleTimes {
}