Fork me on GitHub

Automated testing: Exercise/discussion: Testing using hooks


Teaching: 0 min
Exercises: 15 min
  • How can we use Git hooks to make sure we do not commit broken code?
  • Learn to write a Git hook to automatize testing.

Git hooks

Git hooks are scripts that we can define and which can be run when certain Git events happen. They can, for example, run tests before a commit and reject if it fails.

There are client-side hooks (run on your own computer when committing) and server-side hooks (run on server after pushing, usually used to test if merging is okay).

Type-along: Testing using a pre-commit hook

Here we will use our previous and add a git client-side hook the pre-commit hook to run a script before a commit is recorded.

1. The starting point is our file

We are still in the directory pytest-example which contains

def add(a, b):
    return a + b

def test_add():
    assert add(2, 3) == 5
    assert add('space', 'ship') == 'spaceship'

2. Make sure the test passes

$ pytest

3. Initialize a Git repository

$ git init

4. Git add and commit the file

$ git add
$ git commit

5. Create the following shell script

Save this file as pre-commit:



6. Make pre-commit executable

$ chmod +x pre-commit

7. Move this file under .git/hooks

$ mv pre-commit .git/hooks

8. Break the code and try to commit this change

Now the pre-commit should reject the commit.

9. Discuss

Discuss this approach: pros and cons.

Key points

  • Git hooks provide a convenient way to locally automatize tests.