0

I got stucked in rather unique problem and I'm sure some of you might have faced similar problem at some point.

So I have total 10 Test classes in my testNg suite and I want them to run in parallel. This is easy but the problem is one of my class lets assume class Test A, not only sets up the test data but also tests some functionality of the application, hence I can not use it under BeforeSuite or BeforeTest annotation. The Test data created via the execution of tests under this class is important for other test classes to run, hence I'm really scratching my head how to implement Class level parallelism in such a way that the parallel run only initiate after the tests under Tests A class finishes it's execution.

Summary

Class Test A

public class TestsA {

   @BeforeClass
   public setupTestClass(){
   -----some code--------
  }
  @Test
  public test1(){
 ------some code---
}

I want this TestA class to run first and then instantiate other test classes in parallel. Any help regarding this would be really appreciated.

Thanks

2 Answers2

0

By default all the Test methods will have a priority '0' value. So if you want a specific test method to execute before all the other tests then you could set the priority as -1 for that particular Test. Lesser the priority first will be its execution in testng. So test with priority as -1 precedes all the other tests with priority '0'.

Example Setting priority for a single Test method

public class ClassA {

@Test(priority = -1)
public void b(){
    System.out.println("Test1");
}

@Test
public void a(){
    System.out.println("Test2");
}

}

Example 2: Setting priority for the entire class

@Test(priority = -1)
public class ClassA {


public void b(){
    System.out.println("Test1");
}


public void a(){
    System.out.println("Test2");
}

}

Output For both the examples:

Test1

Test2

Kishore Mohanavelu
  • 439
  • 1
  • 6
  • 17
  • dependsOnMethods and dependsOnGroups are out of the question, as I want my test to be able to run independently, meaning sometimes if I would like to run tests only from Class B I would like that without being have to run tests from Class A. The Test data set up by Class A is have to happen only once for a new environment after that other tests can work independently. It is for those new environment being set up I want the solution for parallely able to run my tests. – Sumit Bhatt May 31 '20 at 20:16
  • 1
    @SumitBhatt Please clarify your requirement or question correctly. You said you wanted to run your test cases after some tests have passed initially in your question. Now you are saying sometimes you want to run your tests independently. Both are completely different things. As per standards, you should not keep the test data setup as a part of the test case. Separate the test data setup from your test case and move it into the BeforeSuite method. So that the test data setup happens before any of the test cases even begins. – Kishore Mohanavelu Jun 01 '20 at 01:57
  • I've mentioned clearly that it's not just Test data set up. The data is being created as part of testing and to perform E2E we need this data to be available for other classes as well. The thing is once the data is there it will be there untill someone deletes it. The Class A tests WILL NOT CREATE DATA IF IT'S ALREADY THERE. Hence we can individually fire any test class after the 1st ever run on a new enviroment completes and test data is created. That's why I want the ability for executing tests from different classes individually without having dependency over Tests on Class A Tests – Sumit Bhatt Jun 01 '20 at 08:01
  • @SumitBhatt correct. So let's assume if the test data is already not there. As you are saying other classes depend on the test data created by Class A right? So this is indeed a dependency correct? Also why is it not possible for you to separate the test data creation as a part of BeforeClass or BeforeSuite or BeforeMethod? Because the sole purpose of BeforeClass, BeforeMethod and BeforeSuite is to act as a pre-requisite for a test case. Your requirement sounds like a pre-requisite. Please provide more details, so that we can provide a proper solution. – Kishore Mohanavelu Jun 01 '20 at 11:07
  • My Requirement is simple. I want execution of Class A Tests first and then Other Test Classes to run in parallel. Is there any way I can achieve this.? – Sumit Bhatt Jun 02 '20 at 05:14
  • @SumitBhatt I updated my answer based on your last comment. If this solves your issue, please accept my answer. – Kishore Mohanavelu Jun 02 '20 at 06:02
  • I tried setting the priority to -1 for the class. It did not work at all infact the class was instantiated last. – Sumit Bhatt Jun 02 '20 at 11:51
0

I've found the solution for the above problem. All I had to do is to have two different tests configured on my testng.xml suite. 1st test will have only Test class A and the other test will have other test classes having parallelism at class level. Since under 1st test there's only one class, the tests under that class will be executed first. In my case Test Class A which is responsible to test some functionality as well as setup some data. Then the next test of your testng.xml will instantiate the other classes in parallel.

Below is how my testng.xml looks


    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

    <suite name="Test_Suite_Name" verbose="1" parallel="classes" thread-count="4">
        <test name="TEST_TESTCLASSA" preserve-order="true">
            <parameter name="environment" value="macos"/>
            <groups>
                <run>
                    <include name="Smoke"/>
                    <exclude name="Sanity"/>
                    <exclude name="Exclude"/>
                </run>
            </groups>
            <classes>
                <class name="testpackage.TestClassA"/>
            </classes>
        </test>
        <test name="TEST_OTHER_TESTS" preserve-order="true">
            <parameter name="environment" value="macos"/>
            <groups>
                <run>
                    <include name="Smoke"/>
                    <exclude name="Sanity"/>
                    <exclude name="Exclude"/>
                </run>
            </groups>
            <classes>
                <class name="testpackage.TestClassB"/>
                <class name ="testpackage.TestClassC"/>
                <class name="testpackage.TestClassD"/>
                <class name="testpackage.TestClassE"/>
                <class name="testpackage.TestClassF"/>
                <class name="testpackage.TestClassG"/>
            </classes>
        </test>
    </suite>