Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running the same JUnit test case multiple time with different data

Tags:

java

junit

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?

like image 299
Giridhar Avatar asked Apr 15 '09 16:04

Giridhar


People also ask

How do you run the same test case multiple times with different data?

First, create a TestNG class file and add all the required annotations. Identify that @Test annotation which you want to run multiple times. Once you identified the @Test annotation then design the test format as below. Here we use keyword named as invocationCount with some numeric value.

How do you run the same test case multiple times in JUnit?

The easiest (as in least amount of new code required) way to do this is to run the test as a parametrized test (annotate with an @RunWith(Parameterized. class) and add a method to provide 10 empty parameters). That way the framework will run the test 10 times.

Which annotation provide ability to run a test multiple times with different arguments?

Parameterized tests make it possible to run a test multiple times with different arguments. They are declared just like regular @Test methods but use the @ParameterizedTest annotation instead.


2 Answers

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}; } 
like image 59
dfa Avatar answered Oct 09 '22 23:10

dfa


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 are some good blog posts about it:

  • Writing a parameterized JUnit test
  • Unit Testing with JUnit 4.0.
like image 35
jjnguy Avatar answered Oct 09 '22 21:10

jjnguy