In that case, Python's unittest allows you to annotate which of your methods in your test case you'd like to skip. Starting from Python 2.7, you could use any of these skip decorators in your code:
1. @unittest.skip
2. @unittest.skipIf
3. @unittest.skipUnless
Here's an example on how to use them -
import sys import unittest class TestSample(unittest.TestCase): @unittest.skip('Unconditional skipping of the test method') def test_skip_unconditional(self): self.assertTrue(True) @unittest.skipUnless('-smoke' in sys.argv,\ 'Skipping the smoke test') def test_skip_unless_conditional(self): self.assertTrue(True) @unittest.skipIf('-quick' in sys.argv,\ 'Skipping a time consuming test') def test_skip_if_conditional(self): self.assertTrue(True) if __name__ == '__main__': # don't pass the args to unittest module try: sys.argv.remove('-quick') except ValueError: pass try: sys.argv.remove('-smoke') except ValueError: pass unittest.main()
The test test_skip_unconditional is skipped all the times. The test test_skip_unless_conditional is run only if the unittest is invoked with -smoke option while the test test_skip_if_conditional is skipped if the unittest is invoked
with -quick option.
Here's the result of running the code above:
$ python sample_tests.py -v
test_skip_if_conditional (__main__.TestSample) ... ok
test_skip_unconditional (__main__.TestSample) ...
skipped 'Unconditional skipping of the test method'
test_skip_unless_conditional (__main__.TestSample) ...
skipped 'Skipping the smoke test'
-------------------------------------------------------------
Ran 3 tests in 0.000s
OK (skipped=2)
As you can see from the result, it shows how many of our tests were skipped and if the test was successful or not. Apart from these commonly used decorators, the unittest module has more and equally useful ones you should check out.
Since discovering Python in the last couple of years, I continue to be blown away at how flexible and refreshing it is to program in a language as cool as this. Unlike Java and other languages, testing modules come as default with your standard Python installation. Apart from this default testing module, you might want to see another one. Pytest.org provides another awesome testing framework that is quite easy to use. The site has a huge documentation to get you started - so check that sites out to learn how to do more with testing your Python code.
But if like me you prefer not to download additional testing framework, then check out Unittest tutorial on Python site for more on how to write Python test code.