代码之家  ›  专栏  ›  技术社区  ›  Adrian Panasiuk

JUnit两个变量迭代各自的值集,创建测试函数

  •  3
  • Adrian Panasiuk  · 技术社区  · 16 年前

    void check(Integer a, Integer b) { ... }
    
    @Test
    public void test_1_2() { check(1, 2); }
    
    ...
    

    class Tests(unittest.TestCase):
        def check(self, i, j):
            self.assertNotEquals(0, i-j)
    
    
    
    for i in xrange(1, 4):
        for j in xrange(2, 6):
            def ch(i, j):
                return lambda self: self.check(i, j)
            setattr(Tests, "test_%r_%r" % (i, j), ch(i, j))
    

    3 回复  |  直到 16 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    @RunWith(Theories.class)
    public class MyTheoryOnUniverseTest {
    
        @DataPoint public static int a1 = 1;
        @DataPoint public static int a2 = 2;
        @DataPoint public static int a3 = 3;
        @DataPoint public static int a4 = 4;
    
        @DataPoint public static int b2 = 2;
        @DataPoint public static int b3 = 3;
        @DataPoint public static int b4 = 4;
        @DataPoint public static int b5 = 5;
        @DataPoint public static int b6 = 6;
    
        @Theory
        public void checkWith(int a, int b) {
            System.out.format("%d, %d\n", a, b);
        }
    }
    

    Testcase: checkWith(MyTheoryOnUniverseTest):        Caused an ERROR
    checkWith(a1, b2) // <--- test failed, like your test_1_2
    

    s 注释:

    @DataPoints public static int as[] = { 1, 2, 3, 4} ;
    @DataPoints public static int bs[] = { 2, 3, 4, 5, 6};
    
    @Theory
    public void checkWith(int a, int b) {
        System.out.format("%d, %d\n", a, b);
    }
    
        2
  •  5
  •   dfa    16 年前

    我会看看JUnit4的 parameterised tests ,并动态生成测试数据数组。

        3
  •  2
  •   Brian Agnew    16 年前

    参数化测试 在JUnit4中。

    JUnit test with dynamic number of tests