I am writing a test suite with pytest to cover an API, with tests organized like this:
class Test_Endpoint:
def test_A(self):
...
def test_B(self):
...
class Test_SubEndpoint:
def test_C(self):
...
Pytest has collected and run tests inside the Test_SubEndpoint class previously, but has suddenly stopped and will only collect and run tests which are not class functions or are members of a root level test class. Previously, test_A test_B and test_C were all collected, but now only test_A and test_B are collected, and I am not aware of any changes I made which would cause this.
It’s also no longer grouping parametrized tests in VSCode’s test viewer, but I’m not sure if that’s an issue with pytest or with VSCode.
The pytest.ini file contents are
[pytest]
addopts = --self-contained-html
filterwarnings =
ignore::DeprecationWarning
pythonpath = .
and were not changed when this problem started happening. When I add explicit definitions for python_files
, python_classes
, and python_functions
which match my naming conventions to the pytest.ini file, it does not affect my issue. My file, class, and function names are in line with the pytest default naming conventions.
Flattening the class structure in my test files does work to get all tests collected, but it loses the organizational aid that I’ve been relying on during development and doesn’t fix the lack of parametrized test grouping, so I’d really prefer to be able to preserve the nested structure.
2
Answers
This appears to have been an issue with the python extension for Visual Studio Code. The extension had updated to
v2023.12.0
, when I discussed this with a colleague and reverted to versionv2023.4.1
at his recommendation, the problem is resolved.Nested test classes are correctly collected and displayed in the side menu testing viewer and when running
pytest --collect-only
in the integrated terminal, and parametrized tests are properly displaying their specified IDs.You can set
pytest
discovery rules in apytest.ini
file:Also, if not using pytest fixtures, you can have your test classes inherit
unittest.TestCase
, which automatically makes them discoverable.