Jayan
Jayan

Reputation: 18459

gradle junit runs: how to pick tests from jar file

We can run junit batchtest with tests picked from a jar file.

<zipfileset src="tests-only.jar" includes="**/*Test.class"/>

This runs all matching tests picked from test-only.jar

How to do similar thing with gradle. The testClassesDir only takes actual directory.

Upvotes: 5

Views: 4255

Answers (3)

keocra
keocra

Reputation: 633

It is possible by treating the jar-File as a zip-File in combination with a custom gradle Test task.

Lets assume the jar-File is on your project main runtimeClasspath and is named "tests-only.jar".

The followin snippet allows gradle to find the classes within the jar-File without actually extracting them:

task testOnlyTests(type: Test) {
  useJUnit()

  // the project runtimeClasspath should include all dependencies of the tests-only.jar file
  classpath = project.sourceSets.main.runtimeClasspath

  testClassesDirs = project.sourceSets.main.output

  // find file tests-only.jar in dependencies or adjust the snippet to find it by other means
  File testJar = classpath.files.find { it.toString().contains("tests-only.jar") }
  testClassesDirs += zipTree(testJar)

  include("**/*Test.class")
}

The same should also be applicable directly to the "test" configuration of your project.

Upvotes: 0

Peter Niederwieser
Peter Niederwieser

Reputation: 123890

Gradle doesn't have a built-in feature for running test classes contained in a Jar file. (Feel free to submit a feature request at http://forums.gradle.org .) What should work is unpacking the Jar (as a separate task) and then setting testClassesDir accordingly. Note that the test classes will also need to be present on the test task's classpath (but it might be good enough to use the Jar for this).

Upvotes: 1

Jared
Jared

Reputation: 39877

See section 23.12.2 of the Gradle user namual. Note this information was found from this question

Upvotes: 1

Related Questions