Debugging with Git and PHPUnit
git bisect can be used to find the change that introduced a bug. It does so by performing a binary search on the list of commits between a known good and a known bad state of the repository. A tool such as PHPUnit can be invoked at each step of the binary search to check whether or not the current state is broken.
Let us assume that the unit tests for our project fail at the current
HEAD of the
sb@ubuntu bankaccount % ant Buildfile: /home/sb/bankaccount/build.xml phpunit: [exec] PHPUnit 3.5.13 by Sebastian Bergmann. [exec] [exec] FSSSS [exec] [exec] Time: 0 seconds, Memory: 4.50Mb [exec] [exec] There was 1 failure: [exec] [exec] 1) BankAccountTest::testBalanceIsInitiallyZero [exec] Failed asserting that <integer:1> matches expected <integer:0>. [exec] [exec] /home/sb/bankaccount/tests/BankAccountTest.php:7 [exec] [exec] FAILURES! [exec] Tests: 1, Assertions: 1, Failures: 1, Skipped: 4. BUILD FAILED
We know that the code worked when we initially imported it. So we need to look up the hash of the initial commit:
sb@ubuntu bankaccount % git log --pretty=oneline 3e83825c0695bcfc1ea38880352b3449f578e4f0 Do not run PHPUnit in verbose mode by default. 48b683a02c54e732a8c3045b21c4f9183343dea8 Remove @covers annotations. d54ba9eb910ce9fe9feafa6b9dbd2675ce43227a Break something. e0c72d7409e461833ce1842e994de4148aa68da4 Remove comment. 75c5d82a31c768d28847d1908cfaa7d15fece4f7 Initial import.
Now we can tell Git that the initial commit was good and that the current state is bad:
sb@ubuntu bankaccount % git bisect start sb@ubuntu bankaccount % git bisect bad sb@ubuntu bankaccount % git bisect good 75c5d82a31c768d28847d1908cfaa7d15fece4f7 Bisecting: 1 revision left to test after this (roughly 1 step) [d54ba9eb910ce9fe9feafa6b9dbd2675ce43227a] Break something.
Next up is actually performing the binary search operation. At each step we use Apache Ant to invoke PHPUnit. The commit will be considered good when all tests pass and bad otherwise.
sb@ubuntu bankaccount % git bisect run ant -q running ant -q BUILD FAILED Bisecting: 0 revisions left to test after this (roughly 0 steps) [e0c72d7409e461833ce1842e994de4148aa68da4] Remove comment. running ant -q BUILD SUCCESSFUL d54ba9eb910ce9fe9feafa6b9dbd2675ce43227a is the first bad commit commit d54ba9eb910ce9fe9feafa6b9dbd2675ce43227a Author: Sebastian Bergmann <firstname.lastname@example.org> Date: Thu Mar 17 10:26:56 2011 +0100 Break something. bisect run success
And this is how you debug using Git and PHPUnit