Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I skip a whole Python 'unittest' module at run time?

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.

like image 541
Jace Browning Avatar asked Sep 19 '12 01:09

Jace Browning


People also ask

What does Unittest main () do?

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.

How do I run a unit test in Python?

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.

How do I skip a test in unittest?

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:

How to import the unittest module only during the test phase?

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

Is it possible to skip tests in Python?

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'.


2 Answers

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) 
like image 175
Mu Mind Avatar answered Oct 06 '22 01:10

Mu Mind


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):             #... 
like image 21
otus Avatar answered Oct 06 '22 01:10

otus