How can I run a test suite using gradle from the c

2020-02-28 02:13发布

问题:

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.

回答1:

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