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
2009-04-15 16:26:18
+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
2009-04-15 16:26:19
+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
2009-04-15 16:34:02
I have few test cases which follows the format like testxyz() ,testpqr() .my existing test class extends TestCase.
Giridhar
2009-04-15 17:58:35
Thos are not running if i am folowing this format.
Giridhar
2009-04-15 17:59:29
first you must convert them to JUnit 4 (by simply using @Test annotation, in most cases)
dfa
2009-04-15 18:27:09
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
2009-04-15 18:39:49
http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/ a nice intro
dfa
2009-04-15 19:23:56
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
2010-02-18 16:38:21