Skip to main content

Running Test Before Push with Git Hooks in Android

·3 mins

In android projects we need test to reduce bugs, verifying the business logic etc. We need to keep tests updated and run them in every iteration to make sure nothing is broken.

In some cases, this process is overlooked. Maybe we are in a hurry to make small bugfix and forget to run tests. This is where CI&CD flows comes in. But not every projects has a full-fledged CI&CD processes, we can start with little bit simple in the early stages in our project. Or we do not want to pollute our commit history for every little mistake.

We can use git hooks to solve this small issue. Even if we forget to do some tasks, we can run some scripts automatically before git push and see if something went wrong or not.

To do that, we need client side git hook scriots.

Here is a pre-push script that I too use in some of my projects;

#!/bin/sh
CMD_TEST="./gradlew clean test"
echo "Running Unit Tests"
$CMD_TEST >/dev/null
TEST_RESULT=$?
if [ $TEST_RESULT -ne 0 ]; then
    echo "failed $CMD_TEST"
    exit 1
fi
exit 0

This script runs after I run the git-push command. It executes before really pushing the commits to remote server depending on exit value. If exit value is 0 push succeeds, if exit value is other than 0(it is 1 in this script) git push command fails.

Git hook mechanism only allows to run a specific script on certain conditions. To run tests in our projects, .gradlew clean test command runs. To not print any output for our tests we need to use >/dev/null.

We check the test result and if it is other than 0 we exit the script with value 1, resulting failing the script and not push the commits.

Adding Hook Scripts to Version Control #

Under normal circumstances git hook scripts are under .git/hooks directory. In fact, if you go to that directory, you can see some sample scripts. Those sample scripts has .sample extension. If you delete the extension, the script will run under those conditions. For example there is a pre-commit.sample script file, if you need to run scripts before committing, just delete the extension and rename the file as pre-commit.

This action needs to be done for every client. If you work in a large team, you might want to commit this scripts rather than sharing by other means.

You can add the hook scripts to the version control, and commit them. Create a .githooks directory, move the scripts to that directory.

  1. Create a .githooks directory
  2. Move the scripts to that directory
  3. Run the command below to change the hook path in git config(git version should be higher than 2.9)
git config --local core.hooksPath .githooks/

Add .githooks directory to version control and commit changes. Then you can push them and share with your teammates. Note that this command needs to be run for every team member(this is a con). You might want to consider a more scalable approach if you work in larger teams(e.g. a git branching model with CI&CD processes).

Results #

This approach allows us to catch broken tests earlier in our production line. Note that this is a simple solution and aims to test before even pushing the commits.

Of course broken test can be catched with a well designed CI&CD processes, but those codes sent to remote already. This way we can avoid polluting commit history.

One downside of this method is that it is coupled with running environment. Best use of this method is single developer or a small team that does not use a significant git branching model.