Skip to content

Instantly share code, notes, and snippets.

@jlnwlf
Created March 20, 2014 09:04
Show Gist options
  • Save jlnwlf/9659899 to your computer and use it in GitHub Desktop.
Save jlnwlf/9659899 to your computer and use it in GitHub Desktop.
git pre-commit hook for Python Unittest + autopep8 check
#!/bin/bash
autopep=$(autopep8 -dr .)
if [[ -z $autopep ]]
then
echo "> PEP8 passed !"
else
echo "> PEP8 DID NOT pass !"
echo "$autopep" | colordiff
exit 1
fi
if [[ `python -m unittest discover .` ]]
then
echo "> Tests DID NOT pass !"
exit 1
else
echo "> Tests passed !"
fi
echo "Ready to commit !"
exit 0
@ojoaldato
Copy link

Hi, thanks for the snippet! Just one thing.

Don't you think the if clause is in the wrong order? Shouldn't it be:

then
    echo "> Tests passed !"
else
    echo "> Tests DID NOT pass !"
    exit 1
fi

Otherwise it would be failing when successful

@vitalybe
Copy link

vitalybe commented Jul 19, 2016

That seems wrong... You're checking the output of the discover command instead of its exit code. Here is my version:

#!/bin/sh

`python -m unittest discover -p "*_test.py"`
if [[ $? = 0 ]]
then
    echo "> Tests passed"
else
    echo "> Tests DID NOT pass"
    exit 1
fi

echo "Ready to commit"
exit 0

@ramitmittal
Copy link

@vitalybe
Thanks for the correction. This works !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment