[Python] Prevent pytest instantiating parent test class

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using the Template Method Pattern for my test cases, i.e. they have the following structure:

    Parent(unittest.TestCase)
    |
    |---- Child1
    |---- Child2
    |---- Child3


    Test cases are defined in the parent. Each child implements a certain different setup.

    When I run my tests with pytest (python -m pytest), I want the inherited tests in Child1 - Child3 to be run, but the ones defined in Parent should not run. They should also not yield a failing test.

    How can I do that?

    I'm using python_classes= in pytest.ini, so tests are not selected based on names. Changing that is not an option.

    I tried:

    • Making Parent abstract. Result: Parent fails to instantiate, tests are marked as failed.
    • Setting __test__ = False in Parent. Result: Child tests are also not executed.

    Continue reading...

Compartilhe esta Página