23

I'm trying to run a single integration tests using gradle's -Dtest.single flag. I have added another source set, src/integrationTest and put the tests in there. I have an integration test task

task integrationTests(type: Test) {
    dependsOn 'assemble', 'integrationTestClasses'    
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
}

This runs fine, but if I try to run a single test it tells me it cannot find a matching test. I don't want to have to run every integration test each time I am writing a new one. Is there a way to do this?

mkobit
  • 43,979
  • 12
  • 156
  • 150
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406

3 Answers3

41

Since Gradle 1.10 you can write:

 //select specific test method
gradle test --tests org.gradle.SomeTest.someFeature

//select specific test class
gradle test --tests org.gradle.SomeTest

//select all tests from package
gradle test --tests org.gradle.internal*

//select all ui test methods from integration tests by naming convention
gradle test --tests *IntegTest*ui*

//selecting tests from different test tasks
gradle test --tests *UiTest integTest --tests *WebTest*ui

Read more here http://www.gradle.org/docs/1.10/release-notes#executing-specific-tests-from-the-command-line

pditommaso
  • 3,186
  • 6
  • 28
  • 43
24

The correct syntax is:

gradle testTaskName -DtestTaskName.single=...

In this case:

gradle integrationTest -DintegrationTest.single=...

Graham
  • 7,431
  • 18
  • 59
  • 84
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • When I try this, I get something like: `$ gradlew -Dtest.single=SingleTest :subproject:test ... :buildSrc:test FAILED FAILURE: Build failed with an exception. What went wrong: Execution failed for task ':test'. Could not find matching test for pattern: SingleTest` – Noel Yap Feb 25 '14 at 17:43
  • Perhaps you don't have a test class with that name? – Peter Niederwieser Feb 25 '14 at 18:13
  • I found that -Dtest.single='foo' was ok on linux but failed on win due to the use of ' vs " or nothing. So, those choices may have impacts due to the shell used. – Peter Kahn Jan 18 '16 at 18:46
  • 1
    Actually, it should be `-DintegrationTest.single=...` (without the "s") – Michał Kosmulski Jun 08 '16 at 08:32
  • Actually it should be -DintegrationTests.single=... because he named the task 'integrationTests' in his gradle file. – Jacques Koorts Feb 07 '17 at 10:42
3

Just incase anyone is coming here looking for answers. This has been removed in gradle 5.0. Look for test.single in https://docs.gradle.org/current/userguide/upgrading_version_4.html

If you still wish to use a command line option in this style you should be able to use the --tests commandline param. See https://docs.gradle.org/current/userguide/java_testing.html#simple_name_pattern

$ ./gradlew integrationTest --tests=MyTest
loosebazooka
  • 2,363
  • 1
  • 21
  • 32