assertEquals("Activity title does not match",expectedTitle,cut.getTitle().toString());
From a directory structure point of view, flavors are realized by adding additional directories to your source folder. I specified the two flavors “free” and “pro”, so I created their related source folders beside the main directory.
Depending on your ‘Build Variants’, the proper flavor directory is highlighted in blue to emphasize which source folders are being checked while programming and considered for device or emulator deployment.
Flavor project src structure
Regarding project structure, beside the test source folders ‘androidTest’ and ‘test‘, you should create test directories matching your product flavor names:
Based on your build variants, you can switch between your flavor based test source folders for execution.
Flavor project test structure
Test Class Naming
Generally known, you cannot add two classes with the same name and package identifier in two different flavors, except asset or resource files. Therefore, your test classes’ names must differ.
Flavor project class naming
If you want to auto-create a (flavored) test case, open your desired class and use the Ctrl + Shift + T shortcut.
Create Flavor Test
After clicking the ‘OK’ button, choose your preferred test (flavor) folder.
Choose Flavor Test Directory
Remark: Depending on your chosen build variant, only the specified flavor test folder will be listed.
In my case, I wanted to test if my FloatingActionButton’s color is properly customized when applying my pro flavor sources. For your interest: I used the BasicActivity example from Android Studio for this demo.
Basic Activity Android Studio – Deep Pink
Basic Activity Android Studio – Red
Both flavors have in common that there activity title matches in both variants. So I implemented a test, located in src/test/MainActivityTest, to assert all common functionality of both flavors whereas separate test classes are located in src/testFree and src/testPro to assert the FloatingActionButton color behavior.
In the end, the flavor test case can be look as follows: