How to Auto Run RSpec Before Pushing to Git

Feature thumb git pre push hook

Being able to run your RSpec tests prior to deploying to Git can save quite a few bugs, such as protecting against breaking your application by deploying a typo. By utilizing Git hooks, you can ensure that you run your test suite before your code gets pushed up to the repository.

To auto run your RSpec tests before  the "git push" command is executed, you can perform the following steps:

1. In your project, create a file called: .git/hooks/pre-push

2. Add the following code (link to source code below the guide as well):


echo "Running RSpec"
bundle exec rspec spec

if [ "$spec" = 0 ]
  echo >&2 "Tests are green, pushing..."
  exit 0
  echo >&2 "Cannot push, tests are failing. Use --no-verify to force push."
  exit 1

3. Make the file executable by running the command: chmod u+x .git/hooks/pre-push