Using pytest_configure
The pytest_configure
hook takes a parameter config, a pytest.Config
object representing the configuration information.
Its a central point useful for configuring the Pytest environment before the test execution begins.
pytest_configure
doesn’t return anything, but you can use the config
object to access and modify the Pytest configuration.
Define Custom Markers with pytest_configure
Custom markers allow you to tag tests with specific labels (slow, fast, API, UI, etc.), which is useful for categorizing and selectively running tests in a large test suite with hundreds of tests.
To speed up your large test suite you can check out this post with 13 proven ways to improve the runtime of your test suite.
So how and where do you define these markers? (you can use them directly in your test functions, but without defining them, you’ll get warnings from Pytest).
tests/example1/conftest.py
def pytest_configure(config):
config.addinivalue_line("markers", "ui: mark test as a UI test")
config.addinivalue_line("markers", "api: mark test as an API test")
Share Global Variables with pytest_configure
Sometimes, you might want to define global variables that are accessible across your test suite. These could be config like settings, environment variables, or even a database connection.
pytest_configure
allows you to add these variables to the config object.
def pytest_configure(config):
config.my_global_data = "Shared Value"
Accessing Command Line Options via pytest_addoption
To truly harness the power of pytest_configure
, you can pair it with the pytest_addoption hook.
The pytest_addoption
hook allows you to add custom command-line options to pytest.
def pytest_addoption(parser):
parser.addoption("--custom-option", action="store", default="default")
def pytest_configure(config):
config.custom_option = config.getoption("--custom-option")
Define Centralized Config via the pytest_configure
hook
As you’ve seen above, pytest_configure
is a powerful hook that allows you to configure the Pytest environment before the test execution begins.
We can use pytest_configure
to read the file and store its contents in the config
object.
import pytest
import os
def pytest_configure(config):
# Get the path to the text file in the same directory as conftest.py
current_directory = os.path.dirname(os.path.abspath(__file__))
input_file_path = os.path.join(current_directory, "input.txt")
# Read the content of the file and store it in the pytest config object
with open(input_file_path, "r") as file:
file_content = file.read()
config._input_file_content = file_content # Store the file content in the pytest config object
@pytest.fixture
def input_file_content(request):
return request.config._input_file_content
pytest_configure
- A Replacement For Fixtures?
Hooks are NOT a replacement for fixtures. Rather a complement to them.
pytest_configure: Best for global, session-wide settings or variables that remain constant. Useful for setting up global variables, custom markers, custom configuration, and so on.
Fixtures: Best for setting up and tearing down resources needed for specific tests, with great flexibility in scope and reusability. Useful for database connections, API clients and so on.
Other Useful Pytest Hooks
While pytest_configure
is a powerful and versatile tool in your arsenal, it’s just the beginning.
Beyond pytest_configure
, there are numerous hooks, each with its specific purpose and versatility:
pytest_addoption: This hook is like a custom command center, allowing you to add your own command-line options to Pytest.
pytest_fixture_setup: This hook lets you set up and tear down conditions before and after your fixtures are called, providing a clean and controlled testing canvas.
pytest_generate_tests: This hook is a master of variation, enabling parameterization of tests at runtime.
You can read more about the different hooks in the official Pytest documentation.