How to call setup once for all tests and disassembly after it's done

I have a bunch of tests written using pytest. There is everything in the dir directory. For instance:

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

A simplified version of the 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 have written the setup_module(module) and teardown_module(module) test_base.py in test_base.py . I expected setup_module to be called once for all tests, and teardown_module() would be called at the end when all tests were completed.

But the functions do not seem to call? Do you need decorators for this?

+14
source share
4 answers

Place setup_module and teardown_module outside the class at the module level. Then add your class to your tests.

 def setup_module(module): """...""" def teardown_module(module): """...""" class TestSomething: def test_dummy(self): """do some tests""" 

See this article for more information.

+4
source

The OP's requirement was that each setup and disassembly was performed only once , and not once per module. This can be done using a combination of the file conftest.py , @pytest.fixture(scope="session") and passing the device name to each test function.

These are described in the Pytest instrument documentation.

Here is 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' 

Output when running 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 cannot give this file a different name and expect Pytest to find it as a data source.

Setting scope="session" is important. Otherwise, the adjustment and dismantling will be repeated for each test module.

If you prefer not to pass the device name my_setup as an argument to each test function, you can put the test functions in a class and apply the pytest.mark.usefixtures decorator to the class.

+4
source

setup_module / teardown_module is called for the module where the possible (derived) tests are defined. It also allows you to customize the setting. If you have only one setup_module, you can put it in test_base.py and import it from other places. NTN.

+1
source

First of all, it is good practice to put all the tests in a module called "tests":

 <product> ... tests/ __init__.py test_something.py 

Secondly, I think you should use the setup_class / teardown_class methods in the base class:

 import unittest class MyBase(unittest.TestCase): @classmethod def setup_class(cls): ... @classmethod def teardown_class(cls): ... 

Additional information: http://pytest.org/latest/xunit_setup.html

-1
source

Source: https://habr.com/ru/post/899408/


All Articles