Running Android Unit Tests before Git Push

This is a quick note about a Git hook I use to double check that everything is fine before pushing to our remote Git repo.

This covers a paranoid scenario, I know, but sometimes a refactoring can go wrong and lose pieces between production code and tests: every now and then, one of my tests goes out-of-sync and when I push to Github/CI, I get some silly error because of a typo somewhere. This trick will re-run every Unit Test when I give git push and will actually push only if tests pass.

The Script

Create a file named pre-push and place it in your_repo/.git/hooks . Be sure the file is executable: chmod +x pre-push

Copy this snippet into the file:

https://gist.github.com/hamen/f080a06b6283c61da637cf81388f4b4e

This uses ./gradlew clean test, but you can use ./gradle check or any other Gradle task you use to run your tests.

Conclusions

Git hooks are quite handy in lots of scenarios and there are plenty of them. Check out http://githooks.com/.

Personally, I find pretty useful my pre-push hook and, obviously, lolcommits! 😀