Java Gradle:如何排除一些测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22209181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:19:31  来源:igfitidea点击:

Gradle: how to exclude some tests?

javaunit-testinggradle

提问by JBT

My src/test/folder includes both unit and functional tests. The classpath of functional tests has the word cucumber, whereas the unit tests do not. So, how can I run the unit tests only?

我的src/test/文件夹包括单元测试和功能测试。功能测试的类路径有单词cucumber,而单元测试没有。那么,我怎样才能只运行单元测试呢?

Thank you very much.

非常感谢。

P.S.: I know it is easy to use the "include" logic to select tests. For example, to only run the functional tests in my case, I can simply use this
./gradlew test -Dtest.single=cucumber/**/
However, I don't know how to exclude tests in a simple way.

PS:我知道使用“包含”逻辑来选择测试很容易。例如,仅在我的情况下运行功能测试,我可以简单地使用它
./gradlew test -Dtest.single=cucumber/**/
但是,我不知道如何以简单的方式排除测试。

BTW, I am using gradle 1.11.

顺便说一句,我正在使用 gradle 1.11。

采纳答案by JB Nizet

The documentationof the task explains it, with an example and everything:

任务的文档通过示例和所有内容对其进行了解释:

apply plugin: 'java' // adds 'test' task

test {
  // ...

  // explicitly include or exclude tests
  include 'org/foo/**'
  exclude 'org/boo/**'

  // ...
}

回答by JBT

Credit: This answer is inspired by JB Nizet's answer. It is posted because it is more direct to my question.

信用:这个答案的灵感来自 JB Nizet 的答案。发布它是因为它更直接地解决了我的问题。

To run the unit tests only, create a new task like this:

要仅运行单元测试,请创建一个新任务,如下所示:

task unitTest( type: Test ) {
    exclude '**/cucumber/**'
}

This way we have:
run all tests: ./gradlew test
run all unit tests: ./gradlew unitTest
run all functional tests: ./gradlew test -Dtest.single=cucumber/**/

这样我们有:
运行所有测试:./gradlew test
运行所有单元测试:./gradlew unitTest
运行所有功能测试:./gradlew test -Dtest.single=cucumber/**/

回答by shabinjo

You can exclude this based on the external system properties.

您可以根据外部系统属性排除此情况。

-Dtest.profile=integration

and in build.gradle

并在 build.gradle 中

test {
    if (System.properties['test.profile'] != 'integration') {
    exclude '**/*integrationTests*'
   }
}

回答by Gayan Weerakutti

You can also define a custom flag in your build.gradle:

您还可以在您的build.gradle:

test {
    if (project.hasProperty('excludeTests')) {
        exclude project.property('excludeTests')
    }
}

Then in the command-line:

然后在命令行中:

gradle test -PexcludeTests=com.test.TestToExclude