这就是我在 groovy 中进行单元测试的方式。
public void testSomeMethod() {
doSomething(1,2,3,4); //this is first test
doSomething(11,22,33,44); //this is second test
}
private void doSomething(a, b, c, d) {
assertEquals(a, actual)
}
基本上,我在同一测试下用不同的值调用 doSomething 2 次。 这可能不是一个好的测试方法,但我只是想尝试一下。
所以,问题是,如果第一个测试失败,第二个测试就不会执行。
有没有办法强制它打印失败消息并继续下一条消息?
最佳答案
是时候使用spock了,您可以在其中进行数据驱动测试,而第二个测试不会被第一个测试门控。您可以像您所要求的那样获得更大的灵 active ,甚至更多。
最终,测试看起来像这样:
void "test something"(){
when:
def result = doSomething(a, b, c, d)
then:
result == expectedResult
where:
a | b | c | d || expectedResult
1 | 2 | 3 | 4 || 100
11 | 22 | 33 | 44 || 1000
}
private doSomething(a, b, c, d){...}
您可以在 spock 框架文档中找到更多详细信息和/或也可以查看 these questions .
顺便说一句,上面的测试示例可以过度简化以使其更优雅。 ;)
void "test something"(){
expect:
result == doSomething(a, b, c, d)
where:
a | b | c | d || result
1 | 2 | 3 | 4 || 100
11 | 22 | 33 | 44 || 1000
}
关于unit-testing - 在一个单元测试下测试多个场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18905491/