I would like my Python unittest module to tell the test runner to skip it entirety under some situations (such as being unable to import a module or locate a critical resource).
I can use @unittest.skipIf(...) to skip a unittest.TestCase class, but how do I skip the entire module? Applying skips to every class is not sufficient because the class definitions themselves could cause exceptions if a module fails to import.
If you look at the definition of
unittest.skipIfandunittest.skip, you can see that the key is doingraise unittest.SkipTest(reason)when the test is executed. If you’re okay with having it show up as one skipped test instead of several in the testrunner, you can simply raiseunittest.SkipTestyourself on import:Running with
nosetests -vgives: