检查函数有两个参数,我想为各个域的笛卡尔积的每个元素都有一个测试用例,但是没有手动编写所有的测试用例.
void check(Integer a, Integer b) { ... }
@Test
public void test_1_2() { check(1, 2); }
...
Run Code Online (Sandbox Code Playgroud)
在python我会去
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))
Run Code Online (Sandbox Code Playgroud)
我怎么能用Java和JUnit做到这一点?
有了理论,你可以写下:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
(使用JUnit 4.5测试)
编辑
理论也会产生很好的错误信息:
Testcase: checkWith(MyTheoryOnUniverseTest): Caused an ERROR
checkWith(a1, b2) // <--- test failed, like your test_1_2
Run Code Online (Sandbox Code Playgroud)
因此,您无需为测试命名,以便轻松识别故障.
EDIT2
或者你可以使用DataPoint 的注释:
@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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1283 次 |
| 最近记录: |