Flavors and instrumentation tests - gradle configu

2020-03-31 03:21发布

问题:

I use flavors in my project and I wanna add add instrumentation tests specific for each flavor.

So I created

MyApplication/src/androidTestFlavor1/java/com.package.test
MyApplication/src/androidTestFlavor2/java/com.package.test

But it doesn't work correctly.

So, I've tried to configure it in build.gradle, I've added

android {
    ...

    sourceSets {
        flavor1{
            instrumentTest.setRoot('src/instrumentationTestFlavor/java')
        }
    }

    ...
}

but I get error:

Error:(59, 0) Could not find property 'instrumentTest' on source set ding.

What's the best solution here?

回答1:

When you setup a new project in android studio it creates a main folder and an androidTest folder. Gradle uses the androidTest folder for common tests across all flavors. See link. So if you try try to write a flavour specific test and put it in the androidTest folder, it will be run against all your flavors and will probably fail.

So if you want to create flavor specific tests you need to add a new test folder for that flavor containing the java folder and your test. The naming convention has to be androidTestFlavorName. See image below.