18
@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

4

4 回答 4

31

使用 JUnit 4.12 (不确定何时引入),可以命名 DataPoints 并将它们分配给参数(我从http://farenda.com/junit/junit-theories-with-datapoints/了解到):

    @RunWith(Theories.class)
    public class TheoriesAndDataPointsTest {
        @DataPoints("a values")
        public static int[] aValues() {
            return new int[]{1, 2};
        }

        @DataPoints("b values")
        public static int[] bValues() {
            return new int[]{3, 4};
        }

        @Theory
        public void theoryForA(@FromDataPoints("a values") int a) {
            System.out.printf("TheoryForA called with a = %d\n", a);
        }

        @Theory
        public void theoryForB(@FromDataPoints("b values") int a) {
            System.out.printf("TheoryForB called with b = %d\n", a);
        }
    }

输出:

TheoryForA called with a = 1
TheoryForA called with a = 2
TheoryForB called with b = 3
TheoryForB called with b = 4
于 2016-03-17T09:59:23.470 回答
23

数据点适用于类。如果您有一个采用 int 的 @Theory 方法,并且您有一个作为 int 数组的 DataPoint,那么它将使用 int 调用它。

@RunWith(Theories.class)
public class TheoryTest {
    @DataPoint public static int input1 = 45;
    @DataPoint public static int input2 = 46;
    @DataPoints public static String[] inputs = new String[] { "foobar", "barbar" };

    @Theory public void testString1(String input) {
        System.out.println("testString1 input=" + input);
    }

    @Theory public void testString2(String input) {
        System.out.println("testString2 input=" + input);
    }

    @Theory public void test1(int input) {
        System.out.println("test1 input=" + input);
    }

    @Theory public void test2(int input) {
        System.out.println("test2 input=" + input);
    }
}

这用 45 和 46 调用 test1,用 45 和 46 调用 test2。它用“foobar”和“barbar”调用 testString1,用“foobar”和“barbar”调用 testString2。

如果你真的想为不同的理论使用不同的数据集,你可以将数据包装在一个私有类中:

@RunWith(Theories.class)
public class TheoryTest {
    public static class I1 { int i; public I1(int i) { this.i = i;} }
    public static class I2 { int i; public I2(int i) { this.i = i;} }

    @DataPoint public static I1 input1 = new I1(45);
    @DataPoint public static I2 input2 = new I2(46);

    @Theory
    public void test1(I1 input) {
        System.out.println("test1 input=" + input.i);
    }

    @Theory
    public void test2(I2 input) {
        System.out.println("test2 input=" + input.i);
    }
}

这用 45 调用 test1,用 46 调用 test2。这可行,但在我看来,它使代码变得模糊,将 Test 类拆分为两个类可能是更好的解决方案。

于 2011-09-26T14:17:51.570 回答
3

参考Gábor Lipták 的回答,命名数据点可以定义为静态字段(参考),它为我们提供更简洁的代码:

    @RunWith(Theories.class)
    public class TheoriesAndDataPointsTest {
        @DataPoints("a values")
        public static int[] aValues = {1, 2};

        @DataPoints("b values")
        public static int[] bValues = {3, 4};

        @Theory
        public void theoryForA(@FromDataPoints("a values") int a) {
            System.out.printf("TheoryForA called with a = %d\n", a);
        }

        @Theory
        public void theoryForB(@FromDataPoints("b values") int a) {
            System.out.printf("TheoryForB called with b = %d\n", a);
        }
    }
于 2018-04-24T06:43:13.947 回答
1

我看到的一些参考文献谈到了使用特定值的测试和验证行为的理论。例如,如果您有一个类具有从属性中添加和减去的方法,则测试将验证结果的正确性(例如,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-specifications.pdf

于 2015-03-18T19:29:15.217 回答