skip to Main Content

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


  1. Chosen as BEST ANSWER

    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 version v2023.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.


  2. You can set pytest discovery rules in a pytest.ini file:

    [pytest]
    # Set pytest discovery rules:
    # (Most of the rules here are similar to the default rules.)
    # (Inheriting unittest.TestCase could override these rules.)
    python_files = test_*.py *_test.py *_tests.py *_suite.py
    python_classes = Test* *Test* *Test *Tests *Suite
    python_functions = test_*
    

    Also, if not using pytest fixtures, you can have your test classes inherit unittest.TestCase, which automatically makes them discoverable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search