Search code examples
javaselenium-webdrivertestngparallel-testing

TestNG: How to run classes in serial and Tests in Parallel?


I have a testng.xml like below,

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite" parallel="tests" thread-count="2">
    <test name="Test1">
        <classes>
            <class name="com.example.FBOneClass" />
            <class name="com.example.FBTwoClass" />
        </classes>
    </test>
    <test name="Test2">
        <classes>
            <class name="com.example.GOneClass" />
            <class name="com.example.GTwoClass" />
        </classes>
    </test>
</suite>

Here, I'm trying to achieve the tests to run in parallel. I can do that by the parallel="tests" thread-count="2"

But the class should run serially! Like all the methods in the com.example.FBOneClass should execute first and then com.example.FBTwoClass methods should run.

What I'm facing is, some of the @Test methods in com.example.FBTwoClass are getting executed before com.example.FBOneClass @Test methods.

How to achieve the expected behaviour?

Thanks in advance 🙏

Note: I did research on it. We can achieve it using the "dependsOn" parameter. But I can't use the com.example.FBTwoClass first method depends on com.example.FBOneClass last method!


Solution

  • You should set parallel="false" in your <test> tag of your suite file. This will ensure that the <test> tags are executed in parallel, but the individual test classes within the <test> tag will run sequentially.

    Note: Also please ensure that you are using the latest released version of TestNG viz.,

    • 7.5.1 - If you are using JDK8
    • 7.8.0 - If you are using JDK11