Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set up and tear down functions for pytest bdd

Tags:

python

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).

like image 237
theQuestionMan Avatar asked Sep 05 '25 18:09

theQuestionMan


1 Answers

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)
like image 171
Turn Avatar answered Sep 08 '25 06:09

Turn