Multiple test suites within Android Studio

2020-07-22 09:37发布

问题:

I have a pure Java sub-project within my Android project. I already have a test suite located within src/test/java, and I wish to introduce a second test suite for integration tests.

I have specified this within my build.gradle as follows:

sourceSets {
    integration {
        java {
            compileClasspath += test.compileClasspath
            runtimeClasspath += test.runtimeClasspath
        }
    }
}

task integrationTest(type: Test, description: 'Runs the integration tests.', group: 'Verification') {
    testClassesDir = sourceSets.integration.output.classesDir
    classpath = sourceSets.integration.runtimeClasspath
}

configurations {
    integrationCompile.extendsFrom testCompile
    integrationRuntime.extendsFrom testRuntime
}

with tests located within src/integration/java. These tests can be run successfully from the command line, and within Android Studio, the java folder appears in green like the main test suite.

But when right-clicking and choosing "Run 'All Tests'", I get the errors No tests were found, Empty test suite..

How can I have a second test suite that can be run easily from within Android Studio?

回答1:

You can try by using SuiteClasses. For example if you have multiple Test classes in a package you can create a Suite.class that is running the suites you want, even all tests as long as you specify them. Then right click and run test on that class. Here is example:

@RunWith(Suite.class)
@SuiteClasses({
        EmailValidatorTest.class,
        NewTest.class })

public class AllTests {

}

Example structure:



回答2:

I've submitted a bug for this: https://issuetracker.google.com/issues/65476541