简体   繁体   中英

How do I make the control flow from one test class to another test class in testNG?

I have 4 test cases and I want the first test case to run from Demo1.java and then run the other 2 test cases from Demo2.java and then return back to Demo1.java to execute the final test case.

Is there any way I can achieve this? Thanks for your help.

  • I have tried priority parameter but I guess that just checks for the priorities in the same test class

Here is a sample code -

Demo1.java

class Demo1{

  @Test
  public void testOne(){ //This case should be executed first
  }

  @Test
  public void testFour(){ //This case should be executed last
  }

}

Demo2.java

class Demo2{

  @Test
  public void testTwo(){ // This case should be executed 2nd in the order
  }

  @Test
  public void testThree(){ // This case should be executed 3rd in the order
  }
}

The combination of @dependsOnGroups and @dependsOnMethods annotations provides the most scalable way of test sequencing across classes and methods in TestNG.

  • @dependsOnGroups will help implement sequencing across classes
  • @dependsOnMethods will help implement sequencing within a class

Here is the working example using @dependsOnGroups and @dependsOnMethods:

Scenario:

  • There are 2 test classes - ATest.java, BTest.java
  • ATest has 2 test methods - A1, A2
  • BTest has 2 test methods - B1, B2
  • The sequence of execution must be: A1, B1, B2, A2

ATest.java

public class ATest.java {

    @Test(groups = "group-a1")

    public void A1() {
        System.out.println("Test A1: Runs before B1");
    }

    @Test(dependsOnGroups = "group-b2")
    public void A2() {
        System.out.println("Test A2: Runs after B2");
    }
}

BTest.java

public class BTest {

    @Test(dependsOnGroups = "group-a1")
    public void B1() {
        System.out.println("Test B1: Runs after A1");
    }

    @Test(dependsOnMethods = "B1", groups = "group-b2")
    public void B2() {
        System.out.println("Test B2: Runs after B1");
    }
}

Note: The above sequencing can be implemented by using just the @dependsOnMethods annotation only.

However, it is not recommended to use @dependsOnMethods across classes, as it would make it mandatory to have unique name for each test case method in the entire suite.

The use of @dependsOnGroups will allow flexibility in test naming, and make it easy to implement sequencing across classes, without hassle.


More information:

https://testng.org/doc/documentation-main.html

Yes you can order Junit test Case by help of

@FixMethodOrder(MethodSorters.JVM)
@TestMethodOrder(Alphanumeric.class)
@TestMethodOrder(OrderAnnotation.class)
@TestMethodOrder(TestCaseLengthOrder.class)

for details you can follow this link click for more detail turtorial

You can achieve by following below approach.

Class TEST1 extends Class TEST2 :

public class TEST1 extends TEST2{
@Test(priority = 1)
public void testOne() {
    System.out.println("Test method one");
}

@Test(priority = 4)
public void testTwo() {
    System.out.println("Test method two");
}

}

Class TEST2 :

@Test(priority = 2)
public void testThree() {
    System.out.println("Test three method in Inherited test");
}

@Test(priority = 3)
public void testFour() {
    System.out.println("Test four method in Inherited test");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM