admin管理员组

文章数量:1125043

does anyone know, whether it is possible to only execute JUnit Tests with specific Tags or annotations, by specifying that on the Gradle CLI without having to specify it in the build file? This article suggests there is a --include-tags option, but Gradle wouldn't accept it.

Background is that I want to create pipeline templates for a specific type of test that will use an annotation I provided. On the annotation I can add the required JUnit Tag. I don't want the projects using the templates having to keep their build file in sync with the pipeline templates.

does anyone know, whether it is possible to only execute JUnit Tests with specific Tags or annotations, by specifying that on the Gradle CLI without having to specify it in the build file? This article suggests there is a --include-tags option, but Gradle wouldn't accept it.

Background is that I want to create pipeline templates for a specific type of test that will use an annotation I provided. On the annotation I can add the required JUnit Tag. I don't want the projects using the templates having to keep their build file in sync with the pipeline templates.

Share Improve this question asked 2 days ago Tim VahlbrockTim Vahlbrock 10110 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

There is indeed no such argument --include-tags for Gradle, the article is misleading. You can however implement this behavior with a Gradle configuration similar to this (in Kotlin):

tasks.test {
    useJUnitPlatform()
    if (project.hasProperty("includeTags")) {
        useJUnitPlatform {
            includeTags(*project.property("includeTags").toString().split(",").toTypedArray())
        }
    }
}

Or, in Groovy:

project.tasks.named("test") {
    useJUnitPlatform()
    if (project.hasProperty('includeTags')) {
        useJUnitPlatform {
            includeTags project.property('includeTags').split(",")
        }
    }
}

Then you could run your tests as follows:

gradle test -PincludeTags=tag1,tag2

As per my knowledge, yes you can filter JUnit tests by tags or annotations directly from the Gradle CLI without modifying the build.gradle file.

You need to use the --tests option to specify the test class or method. While for JUnit 5, the --include-tags option should work for filtering by tags.

An example:

gradle test --tests * --include-tags "yourTag"

Always make sure that you are using JUnit5 and the correct Gradle version that supports this functionality.

本文标签: javaIs it possible to filter jUnit Tests on the Gradle CLI WITHOUT modifying the buildgradleStack Overflow