Java 使用不同的数据多次运行相同的 JUnit 测试用例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/752521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running the same JUnit test case multiple time with different data
提问by Giridhar
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?
有没有办法告诉 JUnit 在继续下一个测试用例之前使用不同的数据连续多次运行特定的测试用例?
采纳答案by dfa
take a look to junit 4.4 theories:
看看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};
}
回答by u7867
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.
我总是只是创建一个根据参数执行测试的辅助方法,然后从 JUnit 测试方法中调用该方法。通常这意味着单个 JUnit 测试方法实际上会执行大量测试,但这对我来说不是问题。如果您想要多个测试方法,每个不同的调用一个,我建议生成测试类。
回答by jjnguy
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:
这里有一些关于它的很好的博客文章:
回答by Kreich
Here is a post I wrote that shows several ways of running the tests repeatedly with code examples:
这是我写的一篇文章,展示了使用代码示例重复运行测试的几种方法:
You can use the @Parametrized runner, or use the special runner included in the post
您可以使用@Parametrized runner,或使用帖子中包含的特殊runner
回答by piotrek
回答by Miklos Jakab
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.
如果您不想或不能使用自定义运行器(例如,您已经在使用其他运行器,例如 Robolectric 运行器),您可以尝试使用此DataSet Rule。
回答by DavidR
A much better way (allows you to have more than one test method) is to use JUnit with JUnitParams:
更好的方法(允许您拥有多个测试方法)是将 JUnit 与 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.
您可以在此处获取 junitparams 项目。