[英]Spock: how can I get a context of the failed test?
def "validate my files list"() {
when:
new File(folder).eachFileRecurse {
validateFile(it)
}
then:
notThrown(ValidationFailedException)
where:
folder << ['folder1', 'folder2']
}
I have coded that simple test with Spock and Groovy. 我已经用Spock和Groovy编写了这个简单的测试代码。 When it fails it prints:
失败时将打印:
Exception itself (...)
异常本身(...)
Expected no exception of type 'ValidationFailedException' to be thrown, but got it nevertheless
预计不会抛出类型为“ ValidationFailedException”的异常,但还是得到了它
I wonder how can I print the fileName and folder1/folder2 parameter to know where the wrong data resides? 我想知道如何打印fileName和folder1 / folder2参数来知道错误的数据在哪里? The only way I see is to create Exception with message containing fileName and folderName and rethrow it from when block, but this is not very convenient.
我看到的唯一方法是用包含fileName和folderName的消息创建Exception并将其从when块中抛出,但这不是很方便。
@Unroll
def "validate #folder"() { ... }
I see no good solutions here. 我在这里看不到任何好的解决方案。
notThrown
method does not return any result with additional information and in my opinion this isn't a case of bad design because the purpose of this method is to validate the result of specification without possibility of any further action. notThrown
方法不会返回带有附加信息的任何结果,而且我认为这不是设计不良的情况,因为此方法的目的是验证规范结果,而无需采取任何进一步的措施。
To be honest I think that adding a detailed message to exception being thrown the best idea. 老实说,我认为最好将详细信息添加到异常中。 Without this message, the exception being thrown seems pointless.
如果没有此消息,则抛出异常似乎毫无意义。 With some details You can easily determine what happened not only inside the test but in the real system as well.
通过一些细节,您不仅可以轻松确定在测试内部发生的情况,还可以轻松确定实际系统中发生的情况。
You can also add println
statement inside eachFileRecurse
closure. 您还可以在
eachFileRecurse
闭包内添加println
语句。 but this obviously a bad idea. 但这显然是一个坏主意。
A workaround is required if you need fileName
but I do not think test like below adds value. 如果您需要
fileName
则需要一种解决方法,但我认为以下测试不会增加价值。 I would rather make sure validateFile()
undertakes the logic of recursively validating each file. 我宁愿确保
validateFile()
承担递归验证每个文件的逻辑。 Therefore, @Peter's approach should be sufficient. 因此,@ Peter的方法应该足够了。 Anyways, here is how the
fileName
can be printed inefficiently, just for the sake of testing: 无论如何,这只是为了进行测试而无法有效打印
fileName
方法:
import spock.lang.*
@Unroll
class SampleSpec extends Specification {
@Shared filesUnderFolder1, filesUnderFolder2
void setupSpec() {
filesUnderFolder1 = []
filesUnderFolder2 = []
new File('folder1').eachFileRecurse {
filesUnderFolder1 << it
}
new File('folder2').eachFileRecurse {
filesUnderFolder2 << it
}
}
void "test validate file #filesUnderTest under folder1"() {
when:
validateFile(filesUnderTest)
then:
notThrown(Exception)
where:
filesUnderTest << filesUnderFolder1
}
void "test validate file #filesUnderTest under folder2"() {
when:
validateFile(filesUnderTest)
then:
thrown(Exception)
where:
filesUnderTest << filesUnderFolder2
}
// Mimic validateFile method call
private validateFile(File file) {
if ( !file.name.startsWith( 'a' ) ) {
throw new Exception()
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.