Search code examples
javaandroidunit-testingandroid-gradle-pluginandroid-sourcesets

Run unit test for main and specific flavours android


I have 5 Flavors mapped to 2 source folders with some common code written in main folder, The b folder has some extra beans for which I have written unit test, now I want to run unit test like all the unit tests in main folders run with some specific unit test written for B app.

1.Where should I place the folder specific unit test for B app. I have trued creating a folder test and placing those test in that folder and adding

unitTest { java.srcDir file('src/b/test') }

in the source set of B app but not this is not working 2.How should I place mapping for unit tests, these are not android tests, I already tired this and this but these seems to be for Android Test not for unit test.

productFlavors {
        staging {
            applicationIdSuffix = ".test"
            resValue "string", "app_name", "A Test"
            //other keys
        }
        prod {
            resValue "string", "app_name", "A"
            //other keys
        }
        dev {
            applicationIdSuffix = ".dev"
            resValue "string", "app_name", "A Dev"
            //other keys
        }

        BStaging {
            applicationIdSuffix = ".b.test"
            resValue "string", "app_name", "B Test"
         //other keys
        }

        BProd {
            applicationIdSuffix = ".b"
            resValue "string", "app_name", "B"
      //other keys
        }
    }

sourceSets {
        prod {
            java.srcDirs = ['src/a/java']
            res.srcDirs = ['src/a/res']
            resources.srcDirs = ['src/a/java']
            manifest.srcFile 'src/a/AndroidManifest.xml'
        }
        dev {
            java.srcDirs = ['src/a/java']
            res.srcDirs = ['src/a/res']
            resources.srcDirs = ['src/a/java']
            manifest.srcFile 'src/a/AndroidManifest.xml'
        }
        staging {
            java.srcDirs = ['src/a/java']
            res.srcDirs = ['src/a/res']
            resources.srcDirs = ['src/a/java']
            manifest.srcFile 'src/a/AndroidManifest.xml'
        }
        BStaging {
            java.srcDirs = ['src/b/java']
            res.srcDirs = ['src/b/res']
            resources.srcDirs = ['src/b/java']
            manifest.srcFile 'src/b/AndroidManifest.xml'
        }
        BProd {
            java.srcDirs = ['src/b/java']
            res.srcDirs = ['src/b/res']
            resources.srcDirs = ['src/b/java']
            manifest.srcFile 'src/b/AndroidManifest.xml'

        }
    }

Solution

  • If you have your product flavour and build type created like this.

    buildTypes {
            release {
                ...
            }
    
            debug {
                debuggable true
            }
        }
    
        productFlavors {
            develop {
    
            }
    
            production {
    
            }
        }
    

    You can run flavour based test using this command in your studio terminal.

    • For develop flavour and debug build : ./gradlew testDevelopDebugUnitTest
    • For develop flavour and release build : ./gradlew testDevelopReleaseUnitTest


    So the pattern goes like test<Flavour><BuildType>UnitTest