使用不同的数据多次运行相同的 JUnit 测试用例

2022-08-31 12:37:53

有没有办法告诉JUnit在进入下一个测试用例之前,使用不同的数据连续多次运行特定的测试用例?


答案 1

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

答案 2

这听起来像是参数化测试的完美候选者。

但是,基本上,参数化测试允许您对不同的数据运行同一组测试。

以下是一些关于它的好博客文章:


推荐