【问题标题】:Get tests to run from gradle commandline获取从 gradle 命令行运行的测试
【发布时间】:2021-06-15 12:52:05
【问题描述】:

我的测试是通过类似 gradle 运行的

./gradlew -Denv=remote clean test --tests '*com.examle.Test*'

在执行测试之前,我需要获取指定要运行的测试的参数 (*com.examle.Test*)。

是否可以在 build.gradle 文件中提取此参数?

我假设是这样的

tasks.withType(Test) {
    def testsToExecute = ??? //need to get "*com.examle.Test*"
}

提前谢谢你。

【问题讨论】:

    标签: java testing gradle


    【解决方案1】:

    如果您想获取命令行中设置的内容,那么似乎没有任何官方方法可以获取该信息。即使您使用internal APIs,这些模式也只能在任务执行时访问(即,还没有在build configuration phase 中)。以下至少适用于 Gradle 7.1:

    tasks.withType(Test) {
        // only at task execution time:
        doFirst {
            // "getCommandLineIncludePatterns()" is part of the internal API only
            def testsToExecute = filter.commandLineIncludePatterns
            // "filter.includePatterns" would use the public API but doesn't contain
            // the patterns from the command line, unfortunately
        }
    }
    

    如果您想对测试进行硬编码以运行,那么您可能正在寻找Test 任务中的the filter method。像下面这样的东西应该可以工作:

    test.filter {
        includeTestsMatching '*com.examle.Test*'
    }
    

    【讨论】:

    • 感谢您的回复!选项 1 似乎很接近,但不幸的是它返回 null。可能是因为我没有明确指定 includeTests/includeCategories 等并且只使用 --tests '*com.examle.Test*' 选项 2 更多的是关于管理要运行的测试 - 我知道但不需要知道。
    • 啊,你是对的,对不起!我应该先测试我的答案;我现在使用经过测试的解决方案对其进行了更新。不幸的是,似乎没有从--tests CLI 选项中获取模式的官方方法。不过,也许您的用例可以通过不同的方式解决,例如,通过使用测试执行侦听器?
    猜你喜欢
    • 2015-08-27
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多