I've not found a definitive answer regarding the interaction between Python unittest features subTest
and skipTest
.
For instance, testing it, it seems that when calling skipTest
within a subTest
context (new in Python 3.4), only the current subTest is skipped. Allowing any loop around the subTest
to continue with other values.
from unittest import TestCase
class NumbersTest(TestCase):
def test_even(self):
"""
Test that numbers between 0 and 5 are all even.
"""
for i in range(0, 6):
with self.subTest(i=i):
if i==3:
self.skipTest("Skip 3.")
self.assertEqual( i%2, 0 )
Output shows that test continues with values 4 and 5 after skipping 3.
test_even (TestUnitSubTest.NumbersTest) ... skipped 'Skip 3.'
======================================================================
FAIL: test_even (TestUnitSubTest.NumbersTest) (i=1)
----------------------------------------------------------------------
Traceback (most recent call last):
File "~/TestUnitSubTest.py", line 14, in test_even
self.assertEqual( i%2, 0 )
AssertionError: 1 != 0
======================================================================
FAIL: test_even (TestUnitSubTest.NumbersTest) (i=5)
----------------------------------------------------------------------
Traceback (most recent call last):
File "~/TestUnitSubTest.py", line 14, in test_even
self.assertEqual( i%2, 0 )
AssertionError: 1 != 0
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (failures=2, skipped=1)
I've not found the definitive answer in Python documentation. It states, by example, that if an assertion fails within a subTest
context, then further subTest
will execute. But I didn't find anything regarding skipping a test.
Can anyone point me to the paragraph I missed?
unittest.TestCase.subTest was originally introduced in Python 3.4 as a lightweight mechanism for test parameterization [1]; it allows you to mark a section of your test as a separate test in its own right using a context manager.
It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase , which may be used to create new test cases. test suite. A test suite is a collection of test cases, test suites, or both.
Third-party unittest frameworks with a lighter-weight syntax for writing tests. For example, assert func(10) == 42. An extensive list of Python testing tools including functional testing frameworks and mock object libraries.
To check which version of the Python library unittest is installed, run pip show unittest or pip3 show unittest in your CMD/Powershell (Windows), or terminal (macOS/Linux/Ubuntu).
I don't have an answer to this per-se, but four years later, ran into the same issue as this question.
It looks like .subTest() and .skipTest() interaction (as well as general logging of subtests) hasn't been fully worked out. https://bugs.python.org/issue25894 and https://bugs.python.org/issue35327 are two open bugs relating to this exact question, opened since 2015 or so.
As of 2020, it looks like both are still open, so this made not be a solved thing yet.
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