djangofan
djangofan

Reputation: 29679

How do I get a JUnit (WebDriver) test to rerun itself a number of times?

This is a question for JUnit in general, but it involves browser automation with Selenium WebDriver.

I have a JUnit4 TestSuite that runs one JUnit test class. My JUnit test class has 10 test methods in it that fill out a webpage. In this JUnit test class I have a List object that holds data for tests. I can currently drive the Unit test by accessing List object like so:

// all data for my tests can be grabbed using the get(0) method
testListObject.getPersonEntity.get(0).getName();

My question for StackOverflow users is:

How do I refactor my JUnit test so that it will run through multiple tests in my Object list?

In other words:

  1. Is it possible to create a method that loops through the list and somehow gets all the test methods to fire off each time in the loop? Would this make sense? Then the unit test wouldn't be finished each time it fills out a page, but instead only after it fills out the page for every test case in the list.

  2. Or, should I move the test list out to the TestSuite object and somehow call the Unit test class multiple times? If so, how?

I am unsure how to accomplish this, not being very familiar with how JUnit works. In other words, not knowing the underpinnings of how the "test" methods get fired off, I don't currently know the proper way to get a JUnit test to run multiple times. Another way to ask: how would I get the TestSuite class to call the unit test class multiple times, each time with a different testentity argument (from a List) that drives the test?

Upvotes: 1

Views: 831

Answers (1)

Kevin Welker
Kevin Welker

Reputation: 7937

See Parameterized and Parameters in JUnit. I think they provide what you need. For a little more help, see this past SO link. You just need to use @RunWith(Parameterized.class), provide a method annotated with Parameterized.Parameters that returns the data needed for each run (in a List, array or some collection), and the weirdest thing is providing a constructor for the test class that takes the input type of your iterable data.

Upvotes: 2

Related Questions