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.
Internally, unittest. main() is using a few tricks to figure out the name of the module (source file) that contains the call to main() . It then imports this modules, examines it, gets a list of all classes and functions which could be tests (according the configuration) and then creates a test case for each of them.
The recommended method to run Unit Tests It is a common practice to keep the testing modules separated from the core application. So we will import the unittest module only during the test phase. Python allows us to do, that, by specifying the -m MODULE_NAME option.
The unittest module allows you to skip a test method or a test class. To skip a test, you have three available options: Use the @unittest.skip () decorator. Call the skipTest () method of the TestCase class. Raise the SkipTest exception. The following example uses the @unittest.skip () decorator to skip the test_case_2 () method unconditionally:
So we will import the unittest module only during the test phase. Python allows us to do, that, by specifying the -m MODULE_NAME option. So, our command will be: python -m unittest -v my_test.py
Support for skipping tests has been added since Python 2.7. It is possible to skip individual test method or TestCase class, conditionally as well as unconditionally. The framework allows a certain test to be marked as an 'expected failure'.
If you look at the definition of unittest.skipIf
and unittest.skip
, you can see that the key is doing raise 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 raise unittest.SkipTest
yourself on import:
import unittest try: # do thing except SomeException: raise unittest.SkipTest("Such-and-such failed. Skipping all tests in foo.py")
Running with nosetests -v
gives:
Failure: SkipTest (Such-and-such failed. Skipping all tests in foo.py) ... SKIP: Such-and-such failed. Skipping all tests in foo.py ---------------------------------------------------------------------- Ran 1 test in 0.002s OK (SKIP=1)
I found that using skipTest in setUp worked well. If you need a module imported, you use a try block to set e.g. module_failed = True, and in setUp call skipTest if it's set. This reports the correct number of test skips with only a short try block needed:
import unittest try: import my_module module_failed = False except ImportError: module_failed = True class MyTests(unittest.TestCase): def setUp(self): if module_failed: self.skipTest('module not tested') def test_something(self): #...
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