How do I correctly setup and teardown for my pytest class with tests? How do I correctly setup and teardown for my pytest class with tests? python python

How do I correctly setup and teardown for my pytest class with tests?


According to Fixture finalization / executing teardown code, the current best practice for setup and teardown is to use yield instead of return:

import pytest@pytest.fixture()def resource():    print("setup")    yield "resource"    print("teardown")class TestResource:    def test_that_depends_on_resource(self, resource):        print("testing {}".format(resource))

Running it results in

$ py.test --capture=no pytest_yield.py=== test session starts ===platform darwin -- Python 2.7.10, pytest-3.0.2, py-1.4.31, pluggy-0.3.1collected 1 itemspytest_yield.py setuptesting resource.teardown=== 1 passed in 0.01 seconds ===

Another way to write teardown code is by accepting a request-context object into your fixture function and calling its request.addfinalizer method with a function that performs the teardown one or multiple times:

import pytest@pytest.fixture()def resource(request):    print("setup")    def teardown():        print("teardown")    request.addfinalizer(teardown)        return "resource"class TestResource:    def test_that_depends_on_resource(self, resource):        print("testing {}".format(resource))


When you write "tests defined as class methods", do you really mean class methods (methods which receive its class as first parameter) or just regular methods (methods which receive an instance as first parameter)?

Since your example uses self for the test methods I'm assuming the latter, so you just need to use setup_method instead:

class Test:    def setup_method(self, test_method):        # configure self.attribute    def teardown_method(self, test_method):        # tear down self.attribute    def test_buttons(self):        # use self.attribute for test

The test method instance is passed to setup_method and teardown_method, but can be ignored if your setup/teardown code doesn't need to know the testing context. More information can be found here.

I also recommend that you familiarize yourself with py.test's fixtures, as they are a more powerful concept.


This might help http://docs.pytest.org/en/latest/xunit_setup.html

In my test suite, I group my test cases into classes. For the setup and teardown I need for all the test cases in that class, I use the setup_class(cls) and teardown_class(cls) classmethods.

And for the setup and teardown I need for each of the test case, I use the setup_method(method) and teardown_method(methods)

Example:

lh = <got log handler from logger module>class TestClass:    @classmethod    def setup_class(cls):        lh.info("starting class: {} execution".format(cls.__name__))    @classmethod    def teardown_class(cls):        lh.info("starting class: {} execution".format(cls.__name__))    def setup_method(self, method):        lh.info("starting execution of tc: {}".format(method.__name__))    def teardown_method(self, method):        lh.info("starting execution of tc: {}".format(method.__name__))    def test_tc1(self):        <tc_content>        assert     def test_tc2(self):        <tc_content>        assert

Now when I run my tests, when the TestClass execution is starting, it logs the details for when it is beginning execution, when it is ending execution and same for the methods..

You can add up other setup and teardown steps you might have in the respective locations.

Hope it helps!