Re: [testng-users] DataProvider test ordering

238 views
Skip to first unread message

Cédric Beust ♔

unread,
Oct 24, 2012, 2:24:13 PM10/24/12
to testng...@googlegroups.com
You will have to invoke first() and then second() yourself in the test method in order to achieve this. The reason behind the ordering you are seeing is that to TestNG, a dependency is satisfied only once the method depended upon has completed, which can only happen once it has received all its sets of parameters.

-- 
Cédric




On Wed, Oct 24, 2012 at 11:02 AM, Brent Ellwein <brent....@gmail.com> wrote:
Hi, I am trying to get the ordering for TestNG to run through an entire instance of a DataProvider before moving onto the next, but am unable to get this to happen.  Here is my test class

import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

@Test(singleThreaded = true)
public class TestNGSample {
    @DataProvider(name = "DataProvider")
    public static Object[][] createSLAs() {
        return new Object[][] {
                               { "Test1", "Test1.1" },
                               { "Test2", "Test2.1" }
        };
    }

    private String one;
    private String two;

    @Factory(dataProvider = "DataProvider")
    public TestNGSample(String val1, String val2) {
        one = val1;
        two = val2;
    }
    @Test
    public void first() {
        System.out.println(one);
    }
    @Test(dependsOnMethods = { "first" })
    public void second() {
        System.out.println(two);
    }
}

When I run this test, the output is

Test1
Test2
Test1.1
Test2.1

However, I would like the output to be

Test1
Test1.1
Test2
Test2.1

Can anyone help me to achieve this?

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/Uwq5DMKf6NoJ.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Brent Ellwein

unread,
Oct 24, 2012, 4:21:11 PM10/24/12
to testng...@googlegroups.com, ced...@beust.com
Unfortunately, this strategy will not meet my requirements.  What I need to be able to do is this:

* always do test 1 to setup an environment
* perform test 2
* perform test 3 even if test 2 fails
* perform test 4 even if 2 and/or 3 fail
etc.

My current approach supports this, other than the trouble that the sets of tests are mixed up.  Were I to use a single method then once any test fails, the others will not be attempted.  Do you have any other suggestions as to how I might achieve this workflow?

Mike

unread,
Oct 25, 2012, 1:47:38 PM10/25/12
to testng...@googlegroups.com, ced...@beust.com
Put them all in the same group and have test 1 use the @BeforeGroups annotation.  The tests within the group will be skipped if test 1 fails, but otherwise they will be independent of each other.  If you need to do some cleanup afterwards you would use @AfterGroups for that.  I make the cleanup method do retries in case it fails and only report the failure as a count unless the last one fails, because in may case having that data in the database affected other tests.

Mike

Brent Ellwein

unread,
Oct 25, 2012, 4:08:01 PM10/25/12
to testng...@googlegroups.com
Mike,

I am unable to add groups to provide the desired behavior.  Can you provide an example by modifying the sample code I provided?

Thanks!
--Brent

To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/AKxU8bCaAJkJ.

Mike

unread,
Nov 7, 2012, 1:52:06 PM11/7/12
to testng...@googlegroups.com
Brent,

I modified your code below and highlighted it in red so you can find it.

Unfortunately, I don't have the example code of how I did it handy, or I would have pasted that in here.

I hope that works for you.

Mike

-- 
Cédric




    @BeforeGroups(groups="first")
    public void first() {
        System.out.println(one);
    }
    @Test(groups="first")
Reply all
Reply to author
Forward
0 new messages