Programing

다른 데이터로 동일한 JUnit 테스트 케이스를 여러 번 실행

lottogame 2020. 10. 22. 07:35
반응형

다른 데이터로 동일한 JUnit 테스트 케이스를 여러 번 실행


다음 테스트 케이스로 넘어 가기 전에 다른 데이터로 특정 테스트 케이스를 여러 번 실행하도록 JUnit에 지시하는 방법이 있습니까?


junit 4.4 이론을 살펴보십시오 .

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

매개 변수화 된 테스트를위한 완벽한 후보 인 것 같습니다.

그러나 기본적으로 매개 변수화 된 테스트를 사용하면 다른 데이터에 대해 동일한 테스트 세트를 실행할 수 있습니다.

다음은 이에 대한 좋은 블로그 게시물입니다.


최근에 조학 프로젝트를 시작했습니다 . 다음과 같이 작성할 수 있습니다.

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

훨씬 더 좋은 방법 (두 개 이상의 테스트 메서드를 가질 수 있음)은 JUnitParams와 함께 JUnit을 사용하는 것입니다.

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

여기서 junitparams 프로젝트를 얻을 수 있습니다 .


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


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.


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


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 DataSet Rule.

참고URL : https://stackoverflow.com/questions/752521/running-the-same-junit-test-case-multiple-time-with-different-data

반응형