8

I have this test class:

class mytest(unittest.TestCase):
    def setUp(self):
        os.mkdir(...)
        ...

    def tearDown(self):
        shutil.rmtree(...)

    def test_one(self):
        ...

    def test_two(self):
        ...

If something fails after mkdir has ran when running setUp of test_one, it will still try to run setUp of test_two. At this point I'll get an error on mkdir because rmtree didn't run.

Is there any way to tell Python unittest to stop running the current test if setUp fails? I'm not looking to stop on a regular test failure.

1
  • it's hacky but you could just catch exceptions in your setUp method and fail accordingly Commented Oct 15, 2011 at 17:38

1 Answer 1

13

Add a failure call in the setUp method.

def setUp(self):
    try:
        somethingThatMightFail()
    except:
        self.fail()
Sign up to request clarification or add additional context in comments.

1 Comment

This only aborts each individual test being set up. I am looking for a way for the circumstances of one setup to abort all other tests and setups. I think the OP wanted that too, but there's some ambiguity in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.