[TIP] pytest: Setup/TearDown with fixtures

Laszlo Papp lpapp at kde.org
Tue Sep 2 07:47:11 PDT 2014


On Tue, Sep 2, 2014 at 2:58 PM, Laszlo Papp <lpapp at kde.org> wrote:
> Right, I think this could be added to the official documentation to
> have a copy/pasteable example in there:
>
> class TestFoo:
>     @classmethod
>     @pytest.fixture(scope="class", autouse = True)
>     def setup(self, request):
>         self.session = foo.session()
>         def tearDown():
>             session.logout()
>         request.addfinalizer(tearDown)

Perhaps this example is even better:

import foo

class TestFoo:
    @classmethod
    @pytest.fixture(scope="class", autouse = True)
    def setup(cls, request):
        cls.session = foo.session()
        cls.session.login('username', 'password')
        def tearDown():
            cls.session.logout()
        request.addfinalizer(tearDown)



More information about the testing-in-python mailing list