Framework Mode - Fixture finalization (request.addfinalizer)
Folder Structure
project_root/ ├── tests/ │ ├── test_example.py │ └── conftest.py ├── src/ │ └── app_code.py └── pytest.ini
project_root/ ├── tests/ │ ├── test_example.py │ └── conftest.py ├── src/ │ └── app_code.py └── pytest.ini
conftest.py, provides setup and teardown using request.addfinalizer.tests/ folder use fixtures for setup and cleanup.src/ folder, tested by tests.pytest.ini for pytest settings and options.Use pytest.ini to configure pytest options like markers and test paths.
Fixtures can accept request object to register finalizers for cleanup after tests.
Example: In conftest.py, define a fixture that sets up a resource and uses request.addfinalizer to release it after test finishes.
Environment variables or command line options can be accessed in fixtures for flexible configuration.
pytest-html for detailed HTML reports.request.addfinalizer for teardown: It ensures cleanup code runs after each test using the fixture.Where in this folder structure would you add a new fixture that opens a database connection and ensures it closes after each test?
Answer: Add the fixture in conftest.py inside the tests/ folder, using request.addfinalizer to close the connection.