Parallelizing classes and method in testng

2019-07-23 16:09发布

问题:

I have a usecase where I have to run certain test methods under one class dynamically.

I am using @factory annotation for generating these test classes dynamically.

I already have my test methods under the mentioned class running in parallel.

How do I make the test classes and test methods both parallel? Is there anyway to do it as well?

public class FactoryClass {

    @Factory
    public Object[] factoryMethod() {
        return new Object[] { new TestClass(), new TestClass() }
    }
}

public class TestClass {

    @DataProvider(name = "firstDataProvider", parallel = true)
    public Object[] firstDataProvider() {
        return new Object[] { };
    }

    @DataProvider(name = "secondDataProvider", parallel = true)
    public Object[] secondDataProvider() {
        return new Object[] { };
    }

    @Test(dataProvider = "firstDataProvider")
    public void firstTestMethod(String arg) {

    }

    @Test(dataProvider = "secondDataProvider")
    public void secondTestMethod(String arg) {

    }
}

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" parallel="methods"  data-provider-thread-count="60">
    <test name="test1">
        <classes>
            <class name="com.amriteya.test.FactoryMain"></class>     
        </classes>
    </test>
</suite>

Following is the layout of my classes.

In test.xml I am setting parallel="methods" but it is not providing the right output for me.

回答1:

You can't set parallel property to both test method and test class. It seems that you need to set parallel tests on a suite level and parallel classes/methods on a test level, here example of testng xml to be clear:

<suite name="Suite1" parallel="tests">
    <test name="test1" parallel="methods">
        <classes>
            <class name="FactoryClass"/>
        </classes>
    </test>
    <test name="test2" parallel="methods">
        <classes>
            <class name="FactoryClass"/>
        </classes>
    </test>
</suite>

All methods will run in unique thread. This case is useful when you want to run tests in parallel for example on several groups of browsers. Parallel option on suite level will define that you have e.g. two pools of browsers (Chrome and Firefox - and you will pass appropriate params to tests), and parellel option on test level will define how many browsers do you have in the pool.