This HOWTO shows using the git pre-commit hook to auto-run all JUnit tests before committing. This can be useful if you have a strict policy that no commit should fail the unit-test suite or if you just want to make sure that your committed changes pass the tests.
The build file
Larger projects usually have a build system. This build system automatically builds and runs your project and tests.
Creating an ant build file from Eclipse
Because many people nowadays use Eclipse, I give you a short introduction on howto create an ant build.xml file and integrate your JUnit tests into it.
- Right click your project in Eclipse and click Export
- Use General/Ant Buildfiles and follow the instructions.
- This will create a build.xml in your project folder which you now have to edit.
- Near the end you will find something like this:
<target name="example"> <mkdir dir="${junit.output.dir}"/> <junit fork="yes" printsummary="withOutAndErr"> <formatter type="xml"/> <test name="example.Test1" todir="${junit.output.dir}"/> <test name="example.Test2" todir="${junit.output.dir}"/> <classpath refid="example.classpath"/> </junit> </target> - Copy these lines and edit the following things:
- Change the target name to test (target name="test")
- Add the dependency: clean,build (target name="test" depends="clean,build")
- Remove the printsummary=”withOutAndErr” attribute. (junit fork="yes")
- Add the attribute haltonfailure=”yes” to the junit tag. (junit fork="yes" haltonfailure="yes")
- Remove the mkdir line.
- Remove the formatter line.
- It should now look something like this:
<target name="test" depends="clean,build"> <junit fork="yes" haltonfailure="yes"> <test name="example.Test1" todir="${junit.output.dir}"/> <test name="example.Test2" todir="${junit.output.dir}"/> <classpath refid="example.classpath"/> </junit> </target> - You can now test your junit tests by running ant test
Using another build system
If you use another build system you are on yourself. But it’s pretty simple. Just make sure you can run a command which fails with a non-zero exit code.
Creating the git hook
Create a file in your git repository .git/hooks/pre-commit and add the following lines:
#!/bin/sh # Run the test suite. # It will exit with 0 if it everything compiled and tested fine. ant test if [ $? -eq 0 ]; then exit 0 else echo "Building your project or running the tests failed." echo "Aborting the commit. Run with --no-verify to ignore." exit 1 fi
If you don’t run Windows you also have to add the executable flag: chmod +x .git/hooks/pre-commit.
Using the git hook
Before committing git will automatically run the test suite now. If it fails the commit will be aborted. If everything runs fine the commit will be created.
If you don’t want to run the test process (probably because you’re currently working on a development feature), you can pass the argument –no-verify to git commit.
Conclusion
This howto is not complete and does not guarantee to work. But it should give you a short introduction on how to do automatic unit-testing before every commit.
