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

后端 未结 1 766
迷失自我
迷失自我 2020-12-22 03:48

all. I have existing xml file testng suite. I want to use it in gradle task and set thread count programmatically



        
相关标签:
1条回答
  • 2020-12-22 04:25

    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

    0 讨论(0)
提交回复
热议问题