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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With