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.
source share