Fixtures are functions that initialize test functions to enable tests executed robustly by providing a steady base. They are used with fixture annotation. In order to determine its scope, a scope parameter is added to the annotation below
- each test (function),
- class,
- session,
- module,
- or package.
#Test data must be created according to the current date/time before each session:
@pytest.fixture(scope="session")
def example_fixture():
#a fixture example that runs before each session to create test data
#In this way, the test data is created automatically right before the session starts without any third-party code execution.
#The user must be login before each test:
@pytest.fixture(scope="function")
def example_fixture():
#a fixture example that runs before each test to login.