TestNG - Factories and Dataproviders

Posted by Tim K on Stack Overflow See other posts from Stack Overflow or by Tim K
Published on 2010-06-01T18:16:33Z Indexed on 2010/06/01 20:53 UTC
Read the original article Hit count: 206

Filed under:
|
|
|
|

Background Story

I'm working at a software firm developing a test automation framework to replace our old spaghetti tangled system.

Since our system requires a login for almost everything we do, I decided it would be best to use @BeforeMethod, @DataProvider, and @Factory to setup my tests. However, I've run into some issues.

Sample Test Case

Lets say the software system is a baseball team roster. We want to test to make sure a user can search for a team member by name.

(Note: I'm aware that BeforeMethods don't run in any given order -- assume that's been taken care of for now.)

@BeforeMethod
public void setupSelenium() {
    // login with username & password
    // acknowledge announcements
    // navigate to search page
}

@Test(dataProvider="players")
public void testSearch(String playerName, String searchTerm) {
    // search for "searchTerm"
    // browse through results
        // pass if we find playerName
    // fail (Didn't find the player)
}

This test case assumes the following:

  • The user has already logged on (in a BeforeMethod, most likely)
  • The user has already navigated to the search page (trivial, before method)
  • The parameters to the test are associated with the aforementioned login

The Problems

So lets try and figure out how to handle the parameters for the test case.

Idea #1

This method allows us to associate dataproviders with usernames, and lets us use multiple users for any specific test case!

@Test(dataProvider="players")
public void testSearch(String user, String pass, String name, String search) {
    // login with user/pass
    // acknowledge announcements
    // navigate to search page
    // ...
}

...but there's lots of repetition, as we have to make EVERY function accept two extra parameters. Not to mention, we're also testing the acknowledge announcements feature, which we don't actually want to test.

Idea #2

So lets use the factory to initialize things properly!

class BaseTestCase {

    public BaseTestCase(String user, String password, Object[][] data);

}

class SomeTest {

    @Factory
    public void ...

}

With this, we end up having to write one factory per test case... Although, it does let us have multiple users per test-case.

Conclusion

I'm about fresh out of ideas. There was another idea I had where I was loading data from an XML file, and then calling the methods from a program... but its getting silly.

Any ideas?

© Stack Overflow or respective owner

Related posts about java

Related posts about unit-testing