How to include/exclude junit5 tags in gradle cmd? - gradle

I want to execute tagged JUnit 5 tests, eg. only slow tests, with gradle.
I want to do the same like this in maven:
mvn test -Dgroups="slow"
But what is the equivalent in gradle? Or is there anything at all?
To execute all JUnit 5 tests which are marked with #Tag("slow"). I know it's quite simple to create a dedicated task like this:
tasks.withType(Test::class.java).configureEach {
useJUnitPlatform() {
includeTags("slow")
}
}
But I have a lot of different tags and I don't want to have a task for each single tag. Or worse, having one task for all permutations.
Another possibility would be to pass self defined properties to the task like this
tasks.withType(Test::class.java).configureEach {
val includeTagsList = System.getProperty("includeTags", "")!!.split(",")
.map { it.trim() }
.filter { it.isNotBlank() }
if (includeTagsList.isNotEmpty()) {
includeTags(*includeTagsList.toTypedArray())
}
}

The last time I checked, Gradle didn't have built-in support for configuring JUnit Platform include and exclude tags via the command line, so you'll have to go with your second approach.
But... there's no need to split, map, and filter the tags: just use a tag expression instead: https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions

You can create a separate task and to choose do you want to run the task or to skip it.
For example add this code in build.gradle:
def slowTests= tasks.register("slowTests", Test) {
useJUnitPlatform {
includeTags "slow"
}
}
Now if you want to run only slow tests:
./gradlew clean build -x test slowTests

Related

Gradle exclude resources for one task but not for second

Is it possible to exclude resources for a task, but do not exclude them for a second task? Both are triggered from a third task like this:
task createBoth {
dependsOn = [createFirst, createSecond]
}
task createFirst {
sourceSets.main.resources.excludes = ['**/images']
}
task createSecond {
sourceSets.main.resources.excludes = []
}
I tried to exclude in doFirst{} method, but it did not work. If run as a single task then it works fine. Both results are the same version of my Java project. Except they have a different main class and one does not need all images. So I want to create both files in a single run. Is it even possible?
Attempt 2:
task createSystemcheckJar(type: OneJar) {
exclude('**/images/ads/images') // And some ohter combinations with stars
}
Same problem.

How to declare project artifacts in non-Java build?

I have multi-project Gradle build that contains also non-Java projects.
I want to declare the artifacts create by one such project in a way that I can use project/configuration dependencies to get them, e.g.
consumer:
dependencies {
myConf project(path: ':producer', configuration: 'myConf')
}
What I currently have is this:
producer:
configurations {
myConf
}
task produceFile {
//... somehow create the file...
outputs.file file('path/to/file')
}
artifacts.add('myConf', produceFile.outputs.files.singleFile, { builtBy produceFile })
Is there a better way to declare the artifact than my clumsy version?
I couldn't figure out a way to pass the task dependency from the artifact to the producing task in one go.
According to the documentation article on Legacy publishing and the javadoc on the ArtifactHandler, for your simple example it should be sufficient to just pass the task, as long as the task type extends AbstractArchiveTask (e.g. Zip or Jar):
artifacts.add('myConf', produceFile)
... or in the more Gradle-ish way:
artifacts {
myConf produceFile
}
The article mentioned above has another example, where a File is passed directly to the add method, which requires you to specify the task to build the file in the way you did in your example.
However, let me propose other ideas for syntax that may be experienced more 'lightweight':
artifacts {
myConf files(produceFile).singleFile { buildBy produceFile }
// or
myConf file: files(produceFile).singleFile, buildBy: [produceFile]
}
These two examples use the Project.files(...) method to resolve the output(s) of the task instead of accessing them manually. The second example makes use of the map syntax often provided by Gradle.
If you want to somehow standardize your way to publish your custom artifacts, I would propose to create a custom task type that offers any of the different arguments the ArtifactHandler can process as a method or property:
class MyTaskType extends DefaultTask {
// ... other stuff ... of course this should be part of a plugin
def getArtifact() {
return ... // either a (Configurable)PublishArtifact (if constructor is available) or a map representation
}
}
task produceFile(type: MyTaskType) {
// configure somehow
}
artifacts {
myConf produceFile.artifact
}

What is the syntax for a gradle subproject task depending on a parent project's task?

I need some help with a Gradle build file. I have some subprojects which depend on tasks in the top folder. I just need to take a test task and split it into two separate test tasks. Currently the file system structure looks like (tab indicates files inside a directory):
top-level-project-folder
A.gradle
B.gradle
C-subproject-folder
C.gradle
D-subproject-folder
D.gradle
Contents of A.gradle (before refactor):
subprojects {
tasks.test.dependsOn {
bTask
}
}
apply from: 'B.gradle'
Contents of C.gradle (before refactor):
test {
...
}
After the refactor, C.gradle needs to look like:
test {
...
}
task runDifferentTests(type : Test) {
...
}
The tricky part is that C.gradle's test task currently depends on bTask. However, after the refactor, C.gradle's test task should not depend on bTask, but the new runDifferentTests task should depend on bTask. (Currently, D.gradle's test task is marked as depending on bTask, but it does not actually depend on it -- I'd like to remove that dependency. The only task in the two subprojects which depends on bTask is the new runDifferentTests task.)
I've tried some different things but can't seem to find a working solution.
Just remove the declaration in subprojects and declare your dependency directly in the subproject, in C.gradle:
runDifferentTests.dependsOn rootProject.bTask
There are a few syntax solutions here:
runDifferentTests.dependsOn (":bTask")
runDifferentTests.dependsOn rootProject.bTask
task runDifferentTests(type : Test, dependsOn: [":bTask"]) {
...
}
task runDifferentTests(type : Test) {
dependsOn rootProject.bTask
...
}
//in the root build.gradle to apply to all subprojects at once.
subprojects {
runDifferentTests.dependsOn (":bTask")
}
runDifferentTests.dependsOn {
tasks.findAll { task -> task.name.startsWith('bTask') }
}
: can be used to go a level up instead of rootProject depends on the preference and the project structure

Gradle task check if property is defined

I have a Gradle task that executes a TestNG test suite.
I want to be able to pass a flag to the task in order to use a special TestNG XML suite file (or just use the default suite if the flag isn't set).
gradle test
... should run the default standard suite of tests
gradle test -Pspecial
... should run the special suite of tests
I've been trying something like this:
test {
if (special) {
test(testng_special.xml);
}
else {
test(testng_default.xml);
}
}
But I get a undefined property error. What is the correct way to go about this?
if (project.hasProperty('special'))
should do it.
Note that what you're doing to select a testng suite won't work, AFAIK: the test task doesn't have any test() method. Refer to https://discuss.gradle.org/t/how-to-run-acceptance-tests-with-testng-from-gradle/4107 for a working example:
test {
useTestNG {
suites 'src/main/resources/testng.xml'
}
}
This worked for me:
test {
if (properties.containsKey('special')) {
test(testng_special.xml);
}
else {
test(testng_default.xml);
}
}
Here are 3 solutions for Kotlin DSL (build.gradle.kts):
val prop = project.properties["myPropName"] ?: "myDefaultValue"
val prop = project.properties["myPropName"] ?: error("Property not found")
if (project.hasProperty("special")) {
val prop = project.properties["myPropName"]
}
Note that you can omit the project. prefix as it is implicit in Gradle build files.
From Gradle Documentation:
-P, --project-prop
Sets a project property of the root project, for example -Pmyprop=myvalue
So you should use:
gradle test -Pspecial=true
with a value after the property name

Gradle Plugin Test UpToDateWhen method

I'm writing a gradle plugin that defines an upToDateWhen closure to skip the task when certain criteria is met. I'm having trouble figuring out how to wrap a test around this method. Currently it looks like:
class MyCoolTask extends DefaultTask {
MyCoolTask() {
outputs.upToDateWhen {
if (somecondition)
return true
else
return false
}
}
}
My test looks like this:
class MyCoolTaskTest {
#Test
void testUpToDateCheck() {
project = ProjectBuilder.builder().build()
project.apply plugin: 'myCoolPlugin'
project.myCoolTask.execute()
// But then how do you do a subsequent run and ensure that the task did not execute?
project.myCoolTask.execute() // running this a second time does not work.
project.myCoolTask.outputs.upToDateWhen() // Throws a syntax error
}
}
Any insight that could be offered would be great! Thanks!
ProjectBuilder is meant for low-level tests that configure the build but don't execute any tasks. You can either factor out the contents of outputs.upToDateWhen { ... } into a method/class and test that, and/or write an acceptance test that executes a real build using the Gradle tooling API.

Categories

Resources