How to use existing testng suite xml file and set thread counts programmatically?

感情迁移 提交于 2019-11-29 18:16:43

The threadcount value specified in the TestNG suite xml file has the final say. That is why even though you try to set it via the TestNGOptions in your gradle test task, it doesnt take effect.

To get past this, you need to do the following:

  • Make sure you are using TestNG v6.11 or higher.
  • Build an implementation of org.testng.IAlterSuiteListener wherein you alter the thread count either at the XmlSuite level (<suite> level) or at the XmlTest level (<test> level)
  • Add a reference to the above created listener into your TestNG suite xml file. Alternatively you can also inject the listener either via the @Listeners annotation (or) via your suite xml (or) via Serviceloaders. For more details read my blog post here
  • Pass on all the System properties that gradle receives to your test task.

Here's how all of this looks like in action.

Test class looks like this

package test;

import org.testng.annotations.Test;

public class ParallelRunner {

    @Test(priority = 1)
    public void a() {
        System.err.println("**" + Thread.currentThread().getId());
    }

    @Test(priority = 2)
    public void b() {
        System.err.println("**" + Thread.currentThread().getId());
    }

    @Test(priority = 3)
    public void c() {
        System.err.println("**" + Thread.currentThread().getId());
    }

    @Test(priority = 4)
    public void d() {
        System.err.println("**" + Thread.currentThread().getId());
    }
}

Test listener looks like this

public class SuiteAlterer implements IAlterSuiteListener {

    @Override
    public void alter(List<XmlSuite> suites) {
        int count = Integer.parseInt(System.getProperty("threadcount", "3"));
        XmlSuite suite = suites.get(0);
        suite.setDataProviderThreadCount(count);
    }
}

Suite xml looks like this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="many_methods_suite" verbose="2">
    <listeners>
        <listener class-name="com.rationaleemotions.SuiteAlterer"/>
    </listeners>
    <test name="many_methods_test" parallel="methods">
        <classes>
            <class name="test.ParallelRunner"/>
        </classes>
    </test>
</suite>

The gradle test task looks like this

test {
    useTestNG() {
        suites 'src/test/resources/krmahadevan.xml'
        systemProperties(System.getProperties())
    }
    testLogging.showStandardStreams = true
}

Here's the output

~/temp/example
23:15 $ gradle -Dthreads=2 clean test

> Task :test

Gradle Test Executor 13 STANDARD_ERROR
    Altered the suite thread count to 2

Gradle Test Executor 13 STANDARD_OUT
    ...
    ... TestNG 6.12 by Cédric Beust (cedric@beust.com)
    ...


many_methods_suite > many_methods_test > test.ParallelRunner.a STANDARD_ERROR
    **14

many_methods_suite > many_methods_test > test.ParallelRunner.b STANDARD_ERROR
    **15

many_methods_suite > many_methods_test > test.ParallelRunner.c STANDARD_ERROR
    **15

many_methods_suite > many_methods_test > test.ParallelRunner.d STANDARD_ERROR
    **15


BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed

You can now control the thread count by passing in an appropriate via the JVM argument -Dthreads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!