Bitbucket pipelines — Run unit tests on pull requests for Android projects
The goal of this article is to trigger a pipeline that will run all unit tests in the project every time a pull request is created or updated.
Now that we have introductions out of the way, let's dive into the steps!
Step 1 — Enabling pipeline inside repository settings
Click on Repository settings 👇
Then click on Settings under the pipeline tab 👇
Then enable the switch for pipelines 👇
Great! Now pipelines have been enabled in our project!
Step 2 — Code for the pipeline script!
Lets now add our script which should run once the pipeline is triggered:
We can create a file bitbucket-pipelines.yml
at the root project level like this 👇
Inside that file, we can add the script as follows 👇
Few things to note:
- We are using the android-30 docker image as the environment where our tests will run.
- We have commented
size: 2x
here as our project is relatively small and doesn’t run out of space when gradle is building. Incase you are running a large project, please uncomment it. - We are using
testDebug
to run the debug unit tests
Running the pipeline
Now once we make a new pull request, we will see in the right-side panel, that the pipeline will begin to execute.
If we go to the pipelines tab from the repository, we can see the status of the pipeline 👇
Once it finishes, and hopefully is successful, you will see the status change to success like 👇
We can even click on the item and go inside to check the steps in detail 👇
VOILA! You are done setting up the pipeline! 👏 🎊 😎
But it is not always rosy! 🌵
Let's see what to do in case you come across some issues.
If you have any .aar files in your project, we need to do some extra changes to get out project running.
Step 1 — Create a new empty directory at the project level for each aar file
Step 2 — Move one of the .aar
files into respective newly created module
Step 3 — Create a build.gradle
file with the following content:
Step 4 — Include this newly created module in your project level settings.gradle
Step 5 — If you have any multiple .aar
files, repeat steps 1 to 4 for each
In case you have multiple .aar
files, it would look like this 👇
Done! That should fix all your build time issues.
If you still have any persistent issues and gradle is not giving you clear logs as to what might cause the issue, please use 👇 in your local terminal to get some insights into the actual cause of the issue
./gradlew clean assembleDebug --warning-mode=all
Hope this article was able to help! Do hit the 👏 to show some ❤️
Multi-module navigation is another interesting article of mine and you can find it here 👇
Follow me on Github for interesting projects.