如何从命令行使用gradle运行测试套件 [英] How can I run a test suite using gradle from the command line

查看:159
本文介绍了如何从命令行使用gradle运行测试套件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gradle使用以下命令运行测试,但它无法正常工作

I'm trying to use gradle to run tests with the following command but its not working

gradle cleanTest test --tests my.package.TestSuite

我的测试套件如下所示

@RunWith(Suite.class)
@Suite.SuiteClasses({
    ATests.class,
    BTests.class,
    CTests.class
})
public class MySuite {
  /* placeholder, use this to contain all integration tests in one spot * */
}

尝试运行以下命令有效,但加重的是,它会运行两次测试。一次,然后再在同一命名空间中的测试套件下

trying to run the following command works, but aggravatingly enough, it runs each test twice. once by itself and then again under the test suite in that same namespace

gradle clean test --tests my.package.*

我可以放弃测试套件这样做,但我想更好地了解这里发生了什么为什么我不能告诉它直接运行测试套件。

I could just drop the test suite and do it this way but I want to better understand whats going on here and why I can't tell it to directly run the test suite.

推荐答案

以下适用于我本地。

gradle -Dtest.single=MySuite clean test

这实际上使用了不同的方法(测试包含)与 - test 使用的更高级过滤方法。

This actually uses a different approach (test inclusion) versus the more advanced filtering approach used by --test.

如引用链接中所述,上面的示例通过创建 ** / MySuite * .class格式的文件包含模式来工作。 code>而 - test 尝试从扫描的测试集中选择测试。我怀疑在Gradle中实现的通用测试过滤与JUnit Suite跑步者的具体案例之间存在一些不可预见的相互作用。

As documented in the referenced link, the example above works by creating a file inclusion pattern of the form **/MySuite*.class whereas --test attempts to select tests from the scanned test set. I suspect there are some unforseen interactions between the generic test filtering implemented in Gradle and the specific cases around the JUnit Suite runner.

话虽如此,即使是Gradle文档警告说上面的方法被取代了,实际上我可能会回应@Opal的评论并定义一个明确的任务来为特定的测试阶段运行套件。例如,以下使用 gradle clean testSuite 运行可能会运行集成套件。

Having said that, even the Gradle docs warn that the above approach is superseded, and in reality I would probably echo @Opal's comment and define an explicit task to run suites for a given phase of testing. For example the following run with gradle clean testSuite might run an integration suite.

task testSuite(type: Test) {
   include 'MySuite.class'  
}

参考文献:

  • https://docs.gradle.org/current/userguide/java_plugin.html#test_filtering

这篇关于如何从命令行使用gradle运行测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆