如何用理论附加DataPoint?

问题描述 投票:17回答:4
@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

junit theory datapoint
4个回答
23
投票

DataPoints适用于该类。如果你有一个@Theory方法接受一个int,并且你有一个DataPoint是一个int数组,那么它将用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);
    }
}

这会调用test1和45,test2调用46.这样可行,但在我看来,它模糊了代码,并且将Test类拆分为两个类可能是更好的解决方案。


26
投票

使用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

3
投票

在引用Gábor Lipták's answer时,可以将命名数据点定义为静态字段(reference),它们为我们提供了更简洁的代码:

    @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);
        }
    }

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

© www.soinside.com 2019 - 2024. All rights reserved.