繁体   English   中英

使用 invocationCount 顺序运行 TestNG 测试

[英]Run TestNG test sequentially with invocationCount

我有一个需要重复运行的长时间运行的 @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>

AnnotationTransformer: public class 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);
    }
}

测试 class:公共 class 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);
    }

}

生成的测试 output 在单独的线程上并行运行测试方法:

IntelliJ_run

如何在开始下一次调用之前完成运行方法? 我知道它会在某个地方的文档中,只是在挣扎。

尝试谷歌:Java 多线程。 它会给你一个解决方案。 可能使用Thread.join(); 和其他东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM