Running the same JUnit test case multiple time with different data

JavaJunit

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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};
}

Solution 2 - Java

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:

Solution 3 - Java

Recently I started zohhak project. It lets you write:

@TestWith({
   "25 USD, 7",
   "38 GBP, 2",
   "null,   0"
})
public void testMethod(Money money, int anotherParameter) {
   ...
}

Solution 4 - Java

A much better way (allows you to have more than one test method) is to use JUnit with JUnitParams:

import junitparams.Parameters;
import org.junit.Test;

@RunWith(JUnitParamsRunner.class)
Public class TestClass() {

    @Test
    @Parameters(method = "generateTestData")
    public void test1(int value, String text, MyObject myObject) {
        ... Test Code ...
    }

    .... Other @Test methods ....

    Object[] generateTestData() {
        MyObject objectA1 = new MyObject();
        MyObject objectA2 = new MyObject();
        MyObject objectA3 = new MyObject();

        return $(
            $(400, "First test text.", objectA1),
            $(402, "Second test text.", objectA2),
            $(403, "Third test text.", objectA3)
        );
    }
}

You can get the junitparams project here.

Solution 5 - Java

Here is a post I wrote that shows several ways of running the tests repeatedly with code examples:

Run a JUnit test repeatedly

You can use the @Parametrized runner, or use the special runner included in the post

Solution 6 - Java

In JUnit 5 you could use @ParameterizedTest and @ValueSource to get a method called multiple times with different input values.

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class PrimeTest {
    @ParameterizedTest
    @ValueSource(ints = {1, 2, 3, 4, 5})
    public void isPrime(int candidate) {
        // called with candidate=1, candidate=2, etc.
    }
}

Solution 7 - Java

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.

Solution 8 - Java

If you are already using a @RunWith in your test class you probably need to take a look at this.

Solution 9 - Java

If you don't want or can't use custom runner (eg. you are already using an other runner, like Robolectric runner), you can try this https://github.com/mikinw/DataSet">DataSet Rule.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGiridhar View Question on Stackoverflow
Solution 1 - JavadfaView Answer on Stackoverflow
Solution 2 - JavajjnguyView Answer on Stackoverflow
Solution 3 - JavapiotrekView Answer on Stackoverflow
Solution 4 - JavaDavidRView Answer on Stackoverflow
Solution 5 - JavaKreichView Answer on Stackoverflow
Solution 6 - JavamaechlerView Answer on Stackoverflow
Solution 7 - JavaMr. Shiny and New 安宇View Answer on Stackoverflow
Solution 8 - Javamike rodentView Answer on Stackoverflow
Solution 9 - JavaMiklos JakabView Answer on Stackoverflow