简体   繁体   English

Spock:如何获取失败测试的上下文?

[英]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.

相关问题 Spring 上下文无法在 Spock 测试方法中加载 - Spring context can not be loaded in Spock test method spock测试中的上下文配置 - Context Configuration in spock test 在Spock中,我该如何在withTransaction中测试代码 - In Spock, how can I test the code inside withTransaction 如何在Spock的setup方法中获取方法? - How i can get the method in the setup method in spock? 如何在ApplicationTestCase中获取测试应用程序本身的上下文 <MyApp> ? - How can I get context of test application itself in ApplicationTestCase<MyApp>? Spock Framework如何测试Spring Boot应用程序上下文 - Spock Framework how to test spring boot application context 我如何在 spock 测试中使用 redisService 我需要清理 redis 数据库以进行设置和清理 - How can i use redisService inside a spock test i need to cleanup the redis database for setup and cleanup 如何解决上下文初始化失败? - How can i resolve context initialization failed? 如何使用 maven/junit/spock 报告测试结果并按功能编号对它们进行分组? - How can I report on test results and group them by feature number using maven/junit/spock? 无法在Spring中获得自定义注释以在Spock集成测试中工作 - Can't get custom annotation in Spring to work in Spock integration test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM