hhrzc
hhrzc

Reputation: 2059

How to run tests in order with datarovider

I need to execute all tests in the order for each dataprovider object. My try:


public class TestTest extends BaseTest {

    private String creds;

    @Factory(dataProvider = "creds")
    TestTest(String creds){
        this.creds = creds;
    }

    @DataProvider
    public static Object[][] creds(){
        return new Object[][]{
                {"Creds1"},
                {"Creds2"}
        };
    }

    @Test()
    public void test1(){
        this.creds = creds;
        Log.info("test1 creds: " + creds);
    }

    @Test(dependsOnMethods = "test1")
    public void test2(){
        Log.info("test2 creds: " + this.creds);
    }
    @Test(dependsOnMethods = "test2")
    public void test3(){
        Log.info("test3 creds: " + this.creds);
    }
    
}


out:

> test1 creds: Creds2 
> test1 creds: Creds1 
> test2 creds: Creds2 
> test2 creds: Creds1
> test3 creds: Creds2
> test3 creds: Creds1

But I need:

> test1 creds: Creds1 
> test2 creds: Creds1 
> test3 creds: Creds1 
> test1 creds: Creds2
> test2 creds: Creds2
> test3 creds: Creds3

Upvotes: 1

Views: 72

Answers (3)

hhrzc
hhrzc

Reputation: 2059

Need add group-by-instances="true" parameter in the xml test file:

    <test name="test" group-by-instances="true">
        <classes>
            <class name="tests.users.TestTest"/>
        </classes>
    </test>

Of course, execution via XML (from maven/Gradle task) will be needed.

Upvotes: 0

Villa_7
Villa_7

Reputation: 547

Try to use @Factory annotation and create object via constructor of a Test class (with needed field values) and invoke all test methods of that class with passed field values.

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109613

The simplest is to remove @Test and make one single new @Test public void test123(). It is a bit more difficult processing failures, but then, the context is more clear: what went before, what is the intended pipeline.

Disadvantage: statistics.

@Test
public void test123() {
    test1();
    test2();
    test3();
}

private void test1() {
    this.creds = creds;
    Log.info("test1 creds: " + creds);
}

private void test2() {
    Log.info("test2 creds: " + this.creds);
}

private void test3() {
    Log.info("test3 creds: " + this.creds);
}

Upvotes: 2

Related Questions