2

In TestNG I have a parameterized Test A which automatically creates n tests from a dataProvider, and a Test B which should be executed each time a test in A finishes as I want to take advantage of the result obtained in A. In other words, I would like to know is it's possible to have the following:

Given a parameterized @Test A(dataProvider = "inputList") and a @Test B, TestNG would create the following unit tests and execute them in the following order:

Test A1
Test B1 (Based on A1 result)
Test A2
Test B2 (Based on B2 result)
...

Test An
Test Bn (Based on An result)

Is it possible with any existing TestNG tag? I know I could treat @Test B as an @After but this wouldn't be understood for TestNG as a test and I need Test B to be seen as a test for later reports.

Nana89
  • 432
  • 4
  • 21
  • If readers are coming from NUnit, they may be familiar with the concept of PNunit's "Synchronized Tests". The answers below appear to help me implement the same concept in TestNG. (Basically I'm just adding some keywords for myself and other's that may be working in both frameworks) – Jefferey Cave Jun 27 '16 at 15:27

2 Answers2

1

You can use a TestNG Factory. e.g.:

On a Factory Method

public class TestClass {
    private final int p1;
    private final int p2;

    public TestClass(int p1, int p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    @Factory(dataProvider = "inputList")
    public static Object[] createTestClasses() {
        return new Object[]{
                new TestClass(1, 1),
                new TestClass(1, 0),
                new TestClass(1, -1),
        };
    }

    @Test
    public void A() {
        // test `A` code, stores result in some class member field
    }

    @Test(dependsOnMethods = {"A"})
    public void B() {
        // test `B` code, uses stored result from test `A`
    }
}

On a Constructor

public class TestClass {
    private final int p1;
    private final int p2;

    @Factory(dataProvider = "inputList")
    public TestClass(int p1, int p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    @DataProvider
    public static Object[][] inputList() {
        return new Object[][]{
                {1, 1},
                {1, 0},
                {1, -1}
        };
    }

    @Test
    public void A() {
        // test `A` code, stores result in some class member field
    }

    @Test(dependsOnMethods = {"A"})
    public void B() {
        // test `B` code, uses stored result from test `A`
    }
}

See Factories in the TestNG documentation page.

mfulton26
  • 29,956
  • 6
  • 64
  • 88
0

Already good answer is provided, if you are just looking forward to execute @Test methods in required order then you can use priority. If you want to specify the dependency between methods, then you can use dependsOnMethods. below is the simple example

  @Test(priority=1)
public void testA1(){

}

@Test(priority=2,dependsOnMethods="testA1")
public void testB1(){

}

@Test(priority=3)
public void testA2(){

}

@Test(priority=4,dependsOnMethods="testA2")
public void testB2(){

}
murali selenium
  • 3,847
  • 2
  • 11
  • 20