如何在TestNG中运行时设置InvocationCount - @Test?

时间:2017-10-29 08:44:52

标签: java testng testng-eclipse

我希望根据业务逻辑多次运行我的Test方法。有没有办法改变InvocationCount来实现相同的目标?任何其他建议也欢迎。

2 个答案:

答案 0 :(得分:3)

您需要基本上利用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 {
}

测试类看起来像这样。

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.stackoverflow.qn46998341.SimpleAnnotationTransformer"/>
    </listeners>
    <test name="46998341_Test">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.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
===============================================

答案 1 :(得分:0)

您可以在课堂上这样做吗?感谢您的帮助,方法级别可以完美地工作,只是想知道我们是否也可以用于类级别

我在下面尝试过,但是没有用。二手TYPE

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @Target({METHOD, TYPE}) public @interface CanRunMultipleTimes { }