Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a test suite using gradle from the command line

Tags:

java

shell

gradle

I'm trying to use gradle to run tests with the following command but its not working

gradle cleanTest test --tests my.package.TestSuite

my test suite looks like the following

@RunWith(Suite.class)
@Suite.SuiteClasses({
    ATests.class,
    BTests.class,
    CTests.class
})
public class MySuite {
  /* placeholder, use this to contain all integration tests in one spot * */
}

trying to run the following command works, but aggravatingly enough, it runs each test twice. once by itself and then again under the test suite in that same namespace

gradle clean test --tests my.package.*

I could just drop the test suite and do it this way but I want to better understand whats going on here and why I can't tell it to directly run the test suite.

like image 940
tam Avatar asked Jun 11 '15 19:06

tam


People also ask

How do I run a Gradle test in command line?

Use the command ./gradlew test to run all tests.

How do I run a Gradle task from the command line?

To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … ​ e.g. gradlew clean allTests.

How do I run a single test case using Gradle?

single system property can be used to specify a single test. You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.

How do I run a program in Gradle?

You can run the application by executing the run task (type: JavaExec). This will compile the main source set, and launch a new JVM with its classes (along with all runtime dependencies) as the classpath and using the specified main class.


1 Answers

The following works for me locally.

gradle -Dtest.single=MySuite clean test

This actually uses a different approach (test inclusion) versus the more advanced filtering approach used by --test.

As documented in the referenced link, the example above works by creating a file inclusion pattern of the form **/MySuite*.class whereas --test attempts to select tests from the scanned test set. I suspect there are some unforseen interactions between the generic test filtering implemented in Gradle and the specific cases around the JUnit Suite runner.

Having said that, even the Gradle docs warn that the above approach is superseded, and in reality I would probably echo @Opal's comment and define an explicit task to run suites for a given phase of testing. For example the following run with gradle clean testSuite might run an integration suite.

task testSuite(type: Test) {
   include 'MySuite.class'  
}

References:

  • https://docs.gradle.org/current/userguide/java_plugin.html#test_filtering
like image 190
lemoncurd Avatar answered Oct 05 '22 18:10

lemoncurd