tags:

views:

1745

answers:

3

Is there there any way to tell JUnit to run a specific test case multiple times with different data continuously before going on to the next test case?

A: 

I always just make a helper method that executes the test based on the parameters, and then call that method from the JUnit test method. Normally this would mean a single JUnit test method would actually execute lots of tests, but that wasn't a problem for me. If you wanted multiple test methods, one for each distinct invocation, I'd recommend generating the test class.

Mr. Shiny and New
+2  A: 

It sounds like that is a perfect candidate for parametrized tests.

But, basically, parametrized tests allow you to run the same set of tests on different data.

Here is a good blog post about it. And another.

jjnguy
+9  A: 

take a look to junit 4.4 theories:

import org.junit.Test;
import org.junit.experimental.theories.*;
import org.junit.runner.RunWith;

@RunWith(Theories.class)
public class PrimeTest {

    @Theory
    public void isPrime(int candidate) {
          // called with candidate=1, candidate=2, etc etc  
    }

    public static @DataPoints int[] candidates = {1, 2, 3, 4, 5};
}
dfa
I have few test cases which follows the format like testxyz() ,testpqr() .my existing test class extends TestCase.
Giridhar
Thos are not running if i am folowing this format.
Giridhar
first you must convert them to JUnit 4 (by simply using @Test annotation, in most cases)
dfa
simply great that works.In this case you had suggested me to use certain annotations.How do i know what annotations are there and what to use with respect to junit?
Giridhar
http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/ a nice intro
dfa
What if I wanted this to act like a set of different tests, so that I may see which data point makes the test fail? (i.e. in your example I would have 4 passing tests and 1 failing, instead of a single failed test)
Manrico Corazzi