[TIP] pytest: Setup/TearDown with fixtures

Laszlo Papp lpapp at kde.org
Tue Sep 2 06:58:23 PDT 2014


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)

I am yet to test this in practice, but this simple snippet would have
been a timer-saver for me when writing this basic functionality ...

On Tue, Sep 2, 2014 at 2:14 PM, Laszlo Papp <lpapp at kde.org> wrote:
> Hi,
>
> I am sorry about bringing up another frustration with the official
> documentation, but ...
>
> It does not seem simple and easy to write a test class and have setup
> and teardown methods with fixtures. The documentation properly
> demonstrates a working version of the "old way"; so far so good.
>
> Yet, I thought being narrow-minded would not be fair, so I gave a try
> to the "new way", but I just failed in every way possible that I have
> tried. I found some chunk code snippets on Stack Overflow and in the
> official documentation, but they are not good.
>
> I could not find any dedicated example explaining how to achieve such
> a simple use case with the "new shiny way". I think the documentation
> would need some improvement in pytest. I simply cannot get my head
> around to this easily, so I just give up for now since I have to
> proceed...
>
> Currently, I am reverting myself to the "old way", but if you
> advertise the "new way" that much, at least give an example for the
> generic and common use case (IMHO) that people, including me, have
> used in other frameworks and languages. Otherwise, it is just
> non-working hype, IMHO. Sorry, I do not like "new shiny ways" that do
> not just work.
>
> Cheers, L.



More information about the testing-in-python mailing list