2

I am curious how Dataprovider runs in a test suite? I mean in which order things run? example:

  • @Dataprovider
  • Test01
  • Test02 -> populates the Dataprovider
  • Test03
  • Test04 -> uses the Dataprovider

Can Test04 run before Test2 and so, use an empty Dataprovider?

Daniel
  • 372
  • 2
  • 12

4 Answers4

1

You can set priority for the each tests. Based on the priority only mentioned for each test cases it will execute

@Test(priority=2)
public void Test1(){
  //...
}


@Test(priority=1)
public void Test2(){
  //...
}
Dan Brandt
  • 605
  • 1
  • 7
  • 21
Mukesh mohan
  • 101
  • 1
  • 6
1

As said @Mukesh, priority can run (start) your Test2 before Test1, but it doesn't garant that Test2 will be completed at moment, when Test1 starting - so, in this approach, your test data can be empty. For your purpose and such garantee you must use groups and dependsOnGroups parameters:

@Test(groups = "groupA")
public void test2() {}

@Test(dependsOnGroups = "groupA")
public void test1() {}

Another moment is that tests, depending on other tests, is not good practice, i think - better to use @Before annotations.

Hope this helps!

Dan Brandt
  • 605
  • 1
  • 7
  • 21
  • 2
    Thanks, but does `Dataprovider` run right before the methods that need it? Or it runs once before all tests(because it is placed at the top of the test class) and again before the test that uses it? – Daniel Oct 05 '18 at 09:23
  • 1
    @Daniel, syntax of `Dataprovider` allow us flexible using it for providing data **straight to test** (its method parameters), that needs them. I'm now adding new comment here with sample and links, see it in a few minutes. – Dan Brandt Oct 05 '18 at 10:13
0

You can use priority as mentioned in above answer. Here is the execution order of priorities, Lower priorities will be scheduled first.

Example: @Test(priority=1) will execute first. @Test(priority=2) will execute second.

Shankar kota
  • 111
  • 6
0

@Daniel, if we need do any prepare actions before running tests (for example, open connection to DB and prepare test data in it, or call other not-testing-here services, or put test data, that will be used in most of all tests) - we must use one of @Before annotations. They can be very useful and flexy, good answer with code about them here. What about @Dataprovider - it provides data straight to tests, those need it:

@Test(dataProvider = "Authentication")
public void errorMessageOnLoginWithBadCredentials(String email, String password, String errMsg) {
            User badUser = new User(email, password);

            at(LoginPage.class)
                    .loginAs(badUser)
                    .errorMessage
                    .shouldHave(exactText(errMsg));
}

@DataProvider(name = "Authentication")
public static Object[][] credentials() {
            return new Object[][]{
                    {" ", " ", "Username is required"},
                    {"user1@gmail.com", "UserTest@123", "Login and / or password do not match"},
                    {"user1@gmail.com", " ", "Password is required"},
                    {"ololo@ololo.com", "admin", "Login and / or password do not match"}
            };
}

To avoid ugly syntax of Object[][] (or Iterator<Object[]>), you can also use @DataSupplier (see here), adapted for using with Stream API, for example.

Hope this be useful.

Dan Brandt
  • 605
  • 1
  • 7
  • 21