Search code examples
pythonbddpytest

Set up and tear down functions for pytest bdd


I'm trying to do setup and teardown modules using pytest-bdd. I know with behave you can do a environment.py file with before_all and after_all modules. How do I do this in pytest-bdd

I have looked into "classic xunit-style setup" plugin and it didn't work when I tried it. (I know thats more related to py-test and not py-test bdd).


Solution

  • You could just declare a pytest.fixture with autouse=true and whatever scope you want. You can then use the request fixture to specify the teardown. E.g.:

    @pytest.fixture(autouse=True, scope='module')
    def setup(request):
    
        # Setup code
    
        def fin():
            # Teardown code
    
        request.addfinalizer(fin)