JUnit理论问题
我正在编写一个测试用例,其中我想为一个测试用例运行一个数据点,为第二个测试用例运行第二个数据点。
@RunWith(Theories.class)
public class DummyTest {
@DataPoints
public static String[] getFileNames() {
return new String[] { "firstFile.txt","firstFile1.txt" };
}
@Theory
public void test1(String fileName) throws Exception {
System.out.println(fileName);
assertThat(true, is(equalTo(Boolean.TRUE)));
}
@DataPoints
public static String[] getSecondFileNames() {
return new String[] { "secondFile.txt","secondFile1.txt" };
}
@Theory
public void test2(String fileName) throws Exception {
System.out.println(fileName);
assertThat(true, is(equalTo(Boolean.TRUE)));
}
}
我希望对于第一个测试用例,我的第一个数据点(即 getFileNames 方法)被调用,对于第二个测试用例,应该调用 getSecondFileNames 数据点。有人能建议这可行吗?
谢谢,
谢卡尔
I am writing a test case where in I want to run a one DataPoint for one test case and second DataPoint for second test case.
@RunWith(Theories.class)
public class DummyTest {
@DataPoints
public static String[] getFileNames() {
return new String[] { "firstFile.txt","firstFile1.txt" };
}
@Theory
public void test1(String fileName) throws Exception {
System.out.println(fileName);
assertThat(true, is(equalTo(Boolean.TRUE)));
}
@DataPoints
public static String[] getSecondFileNames() {
return new String[] { "secondFile.txt","secondFile1.txt" };
}
@Theory
public void test2(String fileName) throws Exception {
System.out.println(fileName);
assertThat(true, is(equalTo(Boolean.TRUE)));
}
}
I want that for first test case my first datapoints i.e. getFileNames method is called and for second test case getSecondFileNames datapoints should be called. Can anybody suggest is this feasible?
Thanks,
Shekhar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从即将推出的 JUnit 4.12 开始,您现在可以命名数据点集并要求参数仅来自该集,例如:
这应该完全解决您的问题:-)。
As of the shortly-upcoming JUnit 4.12, you can now name sets of datapoints and require parameters to come only from that set, e.g.:
This should exactly solve your problem :-).
您可以实现自己的 ParameterSupplier,如下所述: http://blog.schauderhaft.de/2010/02/07/junit-theories/
You could implement your own
ParameterSupplier
, as explained here: http://blog.schauderhaft.de/2010/02/07/junit-theories/测试可以分为“固定装置”,其中固定装置是一组共享相同设置的代码。将使用相同数据点的案例的测试放在同一个类中,这样每组数据就有一个类。
Tests can be grouped into "fixtures", where a fixture is a set of code that shares the same setup. Put the tests for cases using the same datapoints together in the same class, so you have one class for each set of data.
您看过 JUnitParams 吗?如果您只想将参数传递给方法(这就是您的示例所显示的全部内容),那么这是一个更简洁的选择。
Have you looked at JUnitParams? If you just want to pass parameters to methods, which is all that your example shows, it's a much cleaner option.