如何将数据点与理论联系起来?
@DataPoints public static final Integer[] input1={1,2};
@Theory
@Test
public void test1(int input1){
}
@DataPoints public static final Integer[] input2={3,4};
@Theory
@Test
public void test2(int input2 ){
}
我希望 test1 使用数据集 input1 - {1,2} 运行,test2 使用 input2 - {3,4} 运行。但目前每个测试都使用两个数据集 {1,2,3,4} 运行。如何将特定的@DataPoints绑定到特定的@Theorys
@DataPoints public static final Integer[] input1={1,2};
@Theory
@Test
public void test1(int input1){
}
@DataPoints public static final Integer[] input2={3,4};
@Theory
@Test
public void test2(int input2 ){
}
I want that test1 runs with data set input1 - {1,2} and test2 runs with input2 - {3,4}. But currently each test runs with both the data sets {1,2,3,4}. How to bind specific @DataPoints to specific @Theorys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 JUnit 4.12(不确定何时引入),可以命名 DataPoint 并将它们分配给参数(我从 http://farenda.com/junit/junit-theories-with-datapoints/):
输出:
With JUnit 4.12 (not sure when it was introduced) it is possible to name the DataPoints and assign them to parameters (i learned it from http://farenda.com/junit/junit-theories-with-datapoints/):
Output:
数据点适用于该类。如果您有一个采用 int 的 @Theory 方法,并且您有一个作为 int 数组的 DataPoint,那么它将使用 int 进行调用。
这用 45 & 调用 test1 46,而 test2 则为 45 & 46. 它使用“foobar”和“barbar”调用testString1,并使用“foobar”和“barbar”调用testString2。
如果你确实想对不同的理论使用不同的数据集,你可以将数据包装在一个私有类中:
这用 45 调用 test1,用 46 调用 test2。这可行,但在我看来,它使代码变得模糊,并且可能是更好的解决方案是将 Test 类分成两个类。
DataPoints apply to the class. If you have a @Theory method which takes an int, and you have a DataPoint which is an array of ints, then it will be called with the int.
This calls test1 with 45 & 46, and test2 with 45 & 46. It calls testString1 with "foobar" and "barbar" and testString2 with "foobar" and "barbar".
If you really want to use different data sets for different theories, you can wrap the data in a private class:
This calls test1 with 45 and test2 with 46. This works, but in my opinion, it obscures the code, and it may be a better solution to just split the Test class into two classes.
参考Gábor Lipták的回答,命名数据点可以定义为静态字段(参考)这给了我们更简洁的代码:
In reference to Gábor Lipták's answer, named datapoints can be defined as a static fields (reference) which give us more concise code:
我看到的一些参考文献讨论了使用特定值和理论的测试来验证行为。例如,如果您有一个类,该类具有对属性进行加法和减法的方法,则测试将验证结果的正确性(例如,1+3 返回 4),而理论可能会验证,对于数据点值 (x1 , y1), (x2, y2), x+yy 总是等于 x, x-y+y 总是等于 x, x*y/y 总是等于 x 等等。这样,理论的结果就不会与这 数据。借助理论,您还可以过滤掉 y == 0 等情况;他们不算失败。底线:您可以同时使用两者。一篇好的论文是: http:// web.archive.org/web/20110608210825/http://shareandenjoy.saff.net/tdd-specations.pdf
Some of the references I have seen talking about using tests for specific values and theories for verifying behavior. As an example, if you have a class that has methods to add and subtract from an attribute, a test would verify correctness of the result (e.g., 1+3 returns 4) whereas a theory might verify that, for the datapoint values (x1, y1), (x2, y2), x+y-y always equals x, x-y+y always equals x, x*y/y always equals x, etc. This way, the results of theories are not coupled as tightly with the data. With theories, you also can filter out cases such as y == 0; they don't count as failure. Bottom line: you can use both. A good paper is: http://web.archive.org/web/20110608210825/http://shareandenjoy.saff.net/tdd-specifications.pdf