Run tests at build time with Android Studio
So there we were. We’d starting implementing Unit Tests in Android. We were starting to feel more confident in our code, and were infinitely more happy to start making changes around areas of the code that we’d began to test.
The downside was these tests were rarely run because we had to go out of our usualy routine of building the app and remember to build the unit tests seperately. We have a CI server, but ultimately it would be nice to catch those failing tests before pushing and getting to build on Jenkins.
Fortunately Android Studio has helped our plight somewhat by providing a quick way to run the App and the Unit Tests concurrently, so we can keep a constant check on the tests.
- Click on the drop down button next to the play button (Usually says “app”). Click on “Edit Configurations”.
- If not already selected, in the left panel click “Android Application” > “App”.
- In the “Before launch” box, click the plus button. Select “Another configuration” and then select your Unit tests. Click “Ok” on everything after.
And thats it. What you have done is told Gradle to build your Unit tests right after building your app. So every time you build, you are alerted to whether your tests have failed or passed, without having to do anything to your normal build workflow.