In this post, I'm going to walk through how to set up a git commit hook to automatically run your python tests when you commit to git. What's nice is that this hook will also fail the commit if the tests don't pass, ensuring you don't break the build!
So, here's how you set it up. Go to the directory where your repository is. From there, go into the
This is where you can set up all kind of git hooks. From here, we want to create a file called
pre-commit, if you haven't already created one for this repo.
Here's the code that should go in that file:
#!/bin/bash python -m pytest tests if [[ $? = 0]]; then echo "Tests passed! Allowing commit.." else echo "Tests failed! Failing commit.." exit 1 fi
And that's it! As you can see, all you need to do is run your tests via a command like you normally would, then just check the return code from that command.