Question

I am stumped. I have read through many of the threads online and can't seem to find the solution I am looking for.

What I would like is to run tests in a loop ,in parallel, such that one test generates data and the other test consumes the data.

I have found that the ITestContext passed in is useful for storing test data between tests. However, when using dependsOnMethod, all data generation tests run before the first dependency method runs.

Is there a way to specify a combination of @Factory and @DataProvider to achieve what I want?

public class DependencyTest1 {
    private String value;

    @DataProvider()
    public static Object[][] DependencyTestProvider() {
        return new Object[][] {
                // Data passed into tests
                { "String1" },
                { "String2" },
        };
    }

    @Factory(dataProvider = "DependencyTestProvider")
    public DependencyTest1(String value) {

        System.out.println("DataDependencyTest1 Constructor");
        System.out.println("String: " + value);

        this.value = value;

    }

    //*****************************************************************************
    //*****************************************************************************
    @Test(description = "Test Dependency Injection: Generate Test Data", groups = "unit")
    public void Test_DependencyInjection_GenerateTestData(ITestContext context, Method method) throws Exception {

        System.out.println("Test Name: " + method.getName());
        System.out.println("Create Test Data");
        System.out.println("String: " + value);
        System.out.println("Add data to current test context");

        context.setAttribute("value", value);
    }

    //*****************************************************************************
    //*****************************************************************************
    @Test(description = "Test Dependency Injection: Extract Test Data",groups = "unit", dependsOnMethods = "Test_DependencyInjection_GenerateTestData")
    public void Test_DependencyInjection_ExtractData(ITestContext context, Method method) {

        System.out.println("Test Name: " + method.getName());
        System.out.println("Extract data from test context");

        String value = (String) context.getAttribute("value");

        System.out.println("String: " + value);

    }
} 

Test Output:

DataDependencyTest1 Constructor

String: Default test name

DataDependencyTest1 Constructor

String: String1

DataDependencyTest1 Constructor

String: String2

Test Name: Test_DependencyInjection_GenerateTestData

Create Create Test Data

String: String1

Add data to current test context

Test Name: Test_DependencyInjection_GenerateTestData

Create Create Test Data

String: String2

Add data to current test context

Test Name: Test_DependencyInjection_ExtractData

Extract data from test context

String: String2

Test Name: Test_DependencyInjection_ExtractData

Extract data from test context

String: String2

Now here is the problem...

The test runs the _ExtractData() for each time the _GenerateTestData() test method is run. However, the dependency test only seems to run on the last iteration of the loop.

How do I make the test sequence like this:

Test_DependencyInjection_GenerateTestData -- using "String1"

Test_DependencyInjection_ExtractData -- using "String1"

Test_DependencyInjection_GenerateTestData -- using "String2"

Test_DependencyInjection_ExtractData -- using "String2"

No correct solution

OTHER TIPS

Add your tests in separate groups and set group-by-instances to true. And that should do the trick.

Add the attribute "group-by-instances" as "true" of testNG xml and run it, now your each test of a class should be executed first before executing another class instance

<suite name="Suite">
  <test name="Test" group-by-instances="true">
    <classes>
      <class name="Your.test.class.DependencyTest1"/>
    </classes>
  </test> 
</suite>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top