pythonunit-testingpytestxunit

setup/teardown using conftest in pytest


I have different test folders(packages). I want to setup and teardown some data for a specific package(folder).

The problem is set_up() is executed before running the test cases of that folder but after running all the testcases, tear_down is not executing. It's executing after running all the testcases of other packages (folders) also(after whole session of pytest).

     [conftest.py]

     @pytest.fixture(scope="session", autouse=True)
         def set_up(request):
            '''Test package setup'''

         def tear_down():
            '''Test package teardown'''

Each folder contains __init__.py file which is obvious.

So how do i execute the tear_down() just after running all the testcases in that folder for which set_up is executed?

as far i know: scope="module" is useless in this case as i dont want to setup and teardown for each test.

Any help would be great. Thanks


Solution

  • pytest does not directly support package level fixtures. Neither does unittest.

    As for the main test frameworks, I believe nose is the only one to support package fixtures. However, nose2 is dropping package fixture support. See nose2 docs.

    pytest supports module, function, class, and method level fixtures for xunit style fixtures.