Paul Etherton
Paul Etherton

Reputation: 411

Ignore unittests that depend on success of other tests

When writing unit tests, it often happens that some tests sort of "depend" on other tests.

For example, lets suppose I have a test that checks I can instantiate a class. I have other tests that go right ahead and instantiate it and then test other functionality.

Lets also suppose that the class fails to instantiate, for whatever reason.

This results in a ton of tests giving errors. This is bad, because I can't see where the problem really is. What I need is a way of skipping these tests if my instantiation test has failed.

Is there a way of doing this with Python's unittest module?

If this isn't what I should do, what should I do so as to see where the problem really is when something breaks?

Upvotes: 5

Views: 688

Answers (2)

Dirk Herrmann
Dirk Herrmann

Reputation: 5949

I have no suggestion how to avoid running "dependent" tests, but I have a suggestion how you might better live with them: Make the dependencies more apparent and therefore make it easier to analyse test failures later. One simple possibility is the following:

  • In the test-code, you put the tests for the lower-level aspects at the top of the file, and the more dependent tests further to the bottom. Then, when several tests fail, first look at the test that is closest to the top of the file.

Upvotes: 1

Benjamin Hodgson
Benjamin Hodgson

Reputation: 44654

Actually, contrary to my comment above, I think what you need is a setUpClass method. From the docs,

If an exception is raised during a setUpClass then the tests in the class are not run and the tearDownClass is not run. [...] If the exception is a SkipTest exception then the class will be reported as having been skipped instead of as an error.

So something like this should work (I'm sure it could be neater):

class TestMyClass(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # run the constructor test
        if constructor_test_failed:
            raise unittest.SkipTest("Constructor failed")

    def test_other_stuff(self):
        # will get run after setUpClass if it succeeded

Upvotes: 2

Related Questions