Say i have next tests structure:
test/ module1/ test1.py module2/ test2.py module3/ test3.py
How can i setup some method to be called only once before all this tests?
You can use autouse fixtures:
# content of test/conftest.py
import pytest
@pytest.fixture(scope="session", autouse=True)
def execute_before_any_test():
# your setup code goes here, executed ahead of first test
See pytest fixture docs for more info.
If you mean only once in each run of the test suit, then setup and teardown are what you are looking for.
def setup_module(module):
print ("This will at start of module")
def teardown_module(module):
print ("This will run at end of module")
Similarly you can have setup_function and teardown_function which will run at start and end of each test function repetitively. You can also add setup and teardown member function in test classes to run at start and end of the test class run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With