Search code examples
pythonunit-testingnose

Python unittest: cancel all tests if a specific test fails


I am using unittest to test my Flask application, and nose to actually run the tests.

My first set of tests is to ensure the testing environment is clean and prevent running the tests on the Flask app's configured database. I'm confident that I've set up the test environment cleanly, but I'd like some assurance of that without running all the tests.

import unittest

class MyTestCase(unittest.TestCase):
    def setUp(self):
        # set some stuff up
        pass

    def tearDown(self):
        # do the teardown
        pass

class TestEnvironmentTest(MyTestCase):
    def test_environment_is_clean(self):
        # A failing test
        assert 0 == 1

class SomeOtherTest(MyTestCase):
    def test_foo(self):
        # A passing test
        assert 1 == 1

I'd like the TestEnvironmentTest to cause unittest or noseto bail if it fails, and prevent SomeOtherTest and any further tests from running. Is there some built-in method of doing so in either unittest (preferred) or nose that allows for that?


Solution

  • In order to get one test to execute first and only halt execution of the other tests in case of an error with that test, you'll need to put a call to the test in setUp() (because python does not guarantee test order) and then fail or skip the rest on failure.

    I like skipTest() because it actually doesn't run the other tests whereas raising an exception seems to still attempt to run the tests.

    def setUp(self):
        # set some stuff up
        self.environment_is_clean()
    
    def environment_is_clean(self):
        try:
            # A failing test
            assert 0 == 1
        except AssertionError:
            self.skipTest("Test environment is not clean!")