Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 572161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:35:02+00:00 2026-05-13T13:35:02+00:00

I’ve recently started using git, and also begun unit testing (using Python’s unittest module).

  • 0

I’ve recently started using git, and also begun unit testing (using Python’s unittest module). I’d like to run my tests each time I commit, and only commit if they pass.

I’m guessing I need to use pre-commit in /hooks, and I’ve managed to make it run the tests, but I can’t seem to find a way to stop the commit if they tests fail. I’m running the tests with make test, which in turn is running python3.1 foo.py --test. It seems like I don’t get a different exit condition whether the tests pass or fail, but I may be looking in the wrong place.

Edit: Is this something uncommon that I want to do here? I would have thought it was a common requirement…

Edit2: Just in case people can’t be bothered to read the comments, the problem was that unittest.TextTestRunner doesn’t exit with non-zero status, whether the test suite is successful or not. To catch it, I did:

result = runner.run(allTests)
if not result.wasSuccessful():
    sys.exit(1)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:35:02+00:00Added an answer on May 13, 2026 at 1:35 pm

    I would check to make sure that each step of the way, your script returns a non-zero exit code on failure. Check to see if your python3.1 foo.py --test returns a non-zero exit code if a test fails. Check to make sure your make test command returns a non-zero exit code. And finally, check that your pre-commit hook itself returns a non-zero exit code on failure.

    You can check for a non-zero exit code by adding || echo $? to the end of a command; that will print out the exit code if the command failed.

    The following example works for me (I’m redirecting stderr to /dev/null to avoid including too much extraneous output here):

    $ python3.1 test.py 2>/dev/null || echo $?
    1
    $ make test 2>/dev/null || echo $?
    python3.1 test.py
    2
    $ .git/hooks/pre-commit 2>/dev/null || echo $?
    python3.1 test.py
    1
    

    test.py:

    import unittest
    
    class TestFailure(unittest.TestCase):
        def testFail(self):
            assert(False)
    
    if __name__ == '__main__':
        unittest.main()
    

    Makefile:

    test:
        python3.1 test.py
    

    .git/hooks/pre-commit:

    #!/bin/sh
    make test || exit 1
    

    Note the || exit 1. This isn’t necessary if make test is the last command in the hook, as the exit status of the last command will be the exit status of the script. But if you have later checks in your pre-commit hook, then you need to make sure you exit with an error; otherwise, a successful command at the end of the hook will cause your script to exit with a status of 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To add a specific CSS class to some of the… May 14, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer Accelerator keystrokes like tab are handled by the message loop… May 14, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer I was finally able to track down an answer to… May 14, 2026 at 6:53 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.