Problem – To skip or mark selected tests as an anticipated failure in the unit tests.
The unittest module has decorators that can be applied to selected test methods to control their handling as shown in the code given below.
Code #1 :
bash % python3 testsample.py -v test_0 (__main__.Tests) ... ok test_1 (__main__.Tests) ... skipped 'skipped test' test_2 (__main__.Tests) ... skipped 'Not supported on Unix' test_3 (__main__.Tests) ... ok test_4 (__main__.Tests) ... expected failure ---------------------------------------------------------------------- Ran 5 tests in 0.002s OK (skipped = 2, expected failures = 1)
How it works :
- The skip() decorator can be used to skip over a test that need not be run at all.
- skipIf() and skipUnless() can be a useful way to write tests that only apply to certain platforms or Python versions, or which have other dependencies.
Use the @expectedFailure decorator to mark tests that are known failures, but for which the test framework need not report more information.
Code #2 : Applying decorators for skipping methods to entire testing classes
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Kolmogorov-Smirnov Test (KS Test)
- Python - Test if List contains elements in Range
- Python - Test if common values are greater than K
- Analysis of test data using K-Means Clustering in Python
- Python | Test list element similarity
- Python | Test for False list
- Python | Test if string contains element from list
- Python | Test if dictionary contains unique keys and values
- Python | Unit Test Objects Patching | Set-1
- Python | Unit Test Objects Patching | Set-2
- Python | Logging Test Output to a File
- Python | Generate test datasets for Machine learning
- Python | Test for nested list
- Python | Test if all elements are present in list
- Python | Test if any list element returns true for condition
- Python | Test if element is dictionary value
- Python | Test if tuple is distinct
- Python | Test if key exists in tuple keys dictionary
- Python - Test if Tuple contains K
- Python - Test for strictly decreasing list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.