Tales of a Software Craftsman

Running PHPUnit on Git hook

I use Git and I love Git! I knew about Git hooks but never used in real life. So today I decided to get my hands dirty with Git hooks. I had a feeling it’d be awesome if I could use the “pre-commit” hook to do some routine processing before the changes are committed. I had two things in mind: 1) Unit Testing with PHPUnit or 2) Generating documentation using DocBlox. I chose Unit testing favouring simplicity.

Summary: I have a git repo for my PHP project. I have written some unit tests for the project and I want to run an automated unit test before changes are committed. If the unit tests succeed, the changes will be committed, if one of the tests fails, the commit will not proceed. This will ensure that I am always committing *working* codes to my Git repo.

Understanding Git Hooks
Git hooks are nothing but executable scripts which are run by Git on special events. If the hook script returns an exit code of 0, Git continues with whatever it was doing. For any non zero exit code, Git halts the operation. It’s that simple!

Git hooks are stored in the “.git/hooks” directory of a git repo. There are some samples already generated for us to check out! The script should be named with the hook, that is for the “pre-commit” hook the file name should also be “pre-commit”.

My Lame Unit Test
To begin with, we need at least one test case. So I wrote this really lame unit test and saved it as “Mytest.php”

1

2

3

4

5

6

7

8

<?php

classMytestextendsPHPUnit_Framework_TestCase

{

publicfunctiontestSayHello()

{

$this->assertEquals("hello","hello");//lame :D

}

}

Try running the script with PHPUnit like this:

1

phpunit Mytest.php

Output:

1

2

3

4

5

PHPUnit3.6.10by Sebastian Bergmann.

.

Time:0seconds,Memory:4.50Mb

Okay, so we have a valid unit test that passes. Now let’s add a Git pre-commit hook to run this unit test before commit is made.

Setting Up Git Hook
Assuming that your project is already under Git DVCS, navigate to .git/hooks and create a file named “pre-commit” with the following contents:

NB: This is important that you make the script executable otherwise it will not function as a hook.

Making a Commit
We shall create some file or make some changes and try to commit. In my case, I just added the “Mytest.php” file and tried to commit. Here’s the output:

1

2

3

4

5

6

7

8

masnun@ubuntu:~/test$git commit-am"added changes"

+Starting unit tests

+All tests forMy Test passed.

[master3c473de]added changes

1files changed,1insertions(+),1deletions(-)

masnun@ubuntu:~/test$

Aha! All tests passed and the commit was made. Now let’s force the unit test to fail. Change the unit test to look like this:

1

2

3

4

5

6

7

8

<?php

classMytestextendsPHPUnit_Framework_TestCase

{

publicfunctiontestSayHello()

{

$this->assertEquals("hellox","hello");//let's fail - hellox is never equal to hello

}

}

And let’s commit now:

1

2

3

4

5

6

masnun@ubuntu:~/test$git commit-am"this commit should fail"

+Starting unit tests

+Test suite forMy Test failed:

Tests:1,Assertions:1,Failures:1.

masnun@ubuntu:~/test$

Compare the output with previous output. There is nothing similar to:

1

2

[master3c473de]added changes

1files changed,1insertions(+),1deletions(-)

The commit was not made because the test failed. So, it worked! 😀

I am really loving what it is going to offer me! I shall be writing tests for most of my projects so I never unintentionally break something in a commit. Unit testing is just one type of validation, I believe we could use lots of others (eg. PHP Code Sniffer) 🙂