Select pytest as test runner in PyCharm, even if unit tests that use the subTest feature are executed and still get separate items for the subtests in the report.
Background
unittest can only execute unittest tests but not pytest tests.
pytest can execute unittest tests as well as pytest tests.
However, the (unittest specific) subTest feature is not supported, so that the complete tests are aborted as soon as the first subtest fails. There is a pytest plugin pytest-subtests to support subtests. With the plugin, subtests are supported when executing pytest in a shell. However, PyCharm still does not support subtests if pytest is chosen as test runner because the teamcity pytest plugin does not support this feature.
Results
Example script tests/test_it.py:
class MyTest(unittest.TestCase):
def test_this(self):
l = ["a", "b", "c"]
for x in l:
with self.subTest(f"test {x}"):
assert x != "b"
Execute the example tests/test_it.py...
... with test runner unittest:
-> separate sub tests are displayed
... with test runner pytest (without pytest-subtests):
-> no sub tests are displayed
... with test runner pytest (with pytest-subtests, unpatched teamcity):
-> broken end state looks like tests never finish
... with test runner pytest (with pytest-subtests, patched teamcity):
Objective
Select
pytest
as test runner in PyCharm, even if unit tests that use the subTest feature are executed and still get separate items for the subtests in the report.Background
unittest
can only executeunittest
tests but notpytest
tests.pytest
can executeunittest
tests as well aspytest
tests. However, the (unittest
specific)subTest
feature is not supported, so that the complete tests are aborted as soon as the first subtest fails. There is a pytest plugin pytest-subtests to support subtests. With the plugin, subtests are supported when executing pytest in a shell. However, PyCharm still does not support subtests ifpytest
is chosen as test runner because the teamcity pytest plugin does not support this feature.Results
Example script
tests/test_it.py
:Execute the example
tests/test_it.py
...... with test runner
unittest
:-> separate sub tests are displayed
... with test runner
pytest
(withoutpytest-subtests
):-> no sub tests are displayed
... with test runner
pytest
(withpytest-subtests
, unpatchedteamcity
):-> broken end state looks like tests never finish
... with test runner
pytest
(withpytest-subtests
, patchedteamcity
):-> same as with test runner
unittest
🤩