Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call setup once for all tests and teardown after all are finished

I have a bunch of tests written using pytest. There are all under a directory dir. For example:

dir/test_base.py
dir/test_something.py
dir/test_something2.py
...

The simplified version of code in them is as follows:

test_base.py

import pytest

class TestBase:
   def setup_module(module):
      assert False

   def teardown_module(module):
      assert False

test_something.py

import pytest
from test_base import TestBase

class TestSomething(TestBase):
   def test_dummy():
       pass

test_something2.py

import pytest
from test_base import TestBase

class TestSomethingElse(TestBase):
   def test_dummy2():
       pass

All my test_something*.py files extend the base class in test_base.py. Now I wrote setup_module(module) and teardown_module(module) methods in test_base.py. I was expecting the setup_module to be called once for all tests, and teardown_module() to be called at the end, once all tests are finished.

But the functions don’t seem to be getting called? Do I need some decorators for this to work?

like image 401
vinodkone Avatar asked Oct 16 '11 19:10

vinodkone


People also ask

Is teardown called after every test?

Yes, it is called after each testXXX method.

Where will you use setUp and teardown methods and make it run once for all the test in the class?

Discussion. As outlined in Recipe 4.6, JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might want to call a special setup method once before a series of tests, and then call a teardown method once after all tests are complete. The junit.

Where will you use setUp () and teardown () methods?

Prepare and Tear Down State for a Test Class XCTest runs setUp() once before the test class begins. If you need to clean up temporary files or capture any data that you want to analyze after the test class is complete, use the tearDown() class method on XCTestCase .

What are setUp () and teardown () methods in mobile app testing?

For that it has two important methods, setUp() and tearDown() . setUp() — This method is called before the invocation of each test method in the given class. tearDown() — This method is called after the invocation of each test method in given class.


3 Answers

The OP's requirement was for setup and teardown each to execute only once, not one time per module. This can be accomplished with a combination of a conftest.py file, @pytest.fixture(scope="session") and passing the fixture name to each test function.

These are described in the Pytest fixtures documentation

Here's an example:

conftest.py

import pytest

@pytest.fixture(scope="session")
    def my_setup(request):
        print '\nDoing setup'
        def fin():
            print ("\nDoing teardown")
        request.addfinalizer(fin)

test_something.py

def test_dummy(my_setup):
    print '\ntest_dummy'

test_something2.py

def test_dummy2(my_setup):
    print '\ntest_dummy2'

def test_dummy3(my_setup):
    print '\ntest_dummy3'

The output when you run py.test -s:

collected 3 items 

test_something.py 
Doing setup

test_dummy
.
test_something2.py 
test_dummy2
.
test_dummy3
.
Doing teardown

The name conftest.py matters: you can't give this file a different name and expect Pytest to find it as a source of fixtures.

Setting scope="session" is important. Otherwise setup and teardown will be repeated for each test module.

If you'd prefer not to pass the fixture name my_setup as an argument to each test function, you can place test functions inside a class and apply the pytest.mark.usefixtures decorator to the class.

like image 68
Steve Saporta Avatar answered Nov 11 '22 15:11

Steve Saporta


Put setup_module and teardown_module outside of a class on module level. Then add your class with your tests.

def setup_module(module):
    """..."""

def teardown_module(module):
    """..."""

class TestSomething:

    def test_dummy(self):
        """do some tests"""

For more info refer to this article.

like image 42
Jabba Avatar answered Nov 11 '22 13:11

Jabba


setup_module/teardown_module are called for the module where the eventual (derived) tests are defined. This also allows to customize the setup. If you only ever have one setup_module you can put it to test_base.py and import it from the other places. HTH.

like image 23
hpk42 Avatar answered Nov 11 '22 13:11

hpk42