如何将数据点与理论联系起来?

2024-01-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


使用 JUnit 4.12(不知道什么时候引入的)可以命名数据点并将它们分配给参数(我从http://farenda.com/junit/junit-theories-with-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);
        }
    }

Output:

TheoryForA called with a = 1
TheoryForA called with a = 2
TheoryForB called with b = 3
TheoryForB called with b = 4
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将数据点与理论联系起来? 的相关文章

随机推荐