So you decided to write some tests. – Some Simpsons pamphlet.
As your test suite begins to grow, so too do test run times. Fortunately, CircleCI offers parallelization – splitting test suites and running them across multiple containers at once. Unfortunately for the Android community, Gradle isn’t supported right out of the box.
One quick solution is to run unit tests for each build variant on its own container. This can be a quick win when trying to get continuous integration set up with little fuss.
When running test variants on separate containers isn’t enough, these snippets will allow you to parallelize test runs further. That is to say, if you have 5000 tests for a given build variant, you can run ~1000 tests across 5 containers in parallel. Of course at some point you’ll reach a point of diminishing returns, but with this solution you can play with the number of CircleCI containers available to you and it will scale appropriately.
CircleCI’s docs gave me a good starting point, explaining how to split files across the available containers with the environment variables
Because I wasn’t patient enough to wrestle with YAML syntax, I created a balancing.sh script and ran that from my circle.yml file.
In the balancing script I create a list of files for our given CircleCI container and pass them as a string to the Gradle test task as the property
Finally, we need to handle this property in our build file.
The intention here is that we want to run all the tests in the suite when there is no property present (which would be typical of local test run) but run only the tests passed in with the property when it’s present.
Voila! Parallelization achieved!