conftest.py
pytest's conftest.py
is a configuration file used by pytest for test collection, test execution, and reporting results. conftest.py
is automatically detected during pytest execution and can define fixtures, plugins, command line options, and customized test runners that pytest will use during test collection and execution.
The conftest.py
can be placed in the same directory as the test files. You can also define fixtures that are shared by multiple test files by placing them in the parent directory of the directory containing the test files.
The following is an example of defining a fixture in conftest.py
.
import pytest
@pytest.fixture
def my_fixture():
return 'Hello, pytest!'
Thus, a fixture can be defined in conftest.py
using the @pytest.fixture
decorator. The above example defines a fixture named my_fixture
.
Define a fixture to be shared by multiple test files
The following is an example of using conftest.py to define a fixture that will be shared by multiple test files.
import pytest
@pytest.fixture
def my_fixture():
return 'Hello, pytest!'
def test_my_fixture(my_fixture):
assert my_fixture == 'Hello, pytest!'
def test_my_fixture(my_fixture):
assert my_fixture == 'Hello, pytest!'
In the above example, a fixture named my_fixture
defined in conftest.py
is used in test_file1.py
and test_file2.py
. This allows you to define a fixture that is shared by multiple test files.
conftest.py and pytest hook function
The following is an example of using conftest.py
and the pytest hook function.
Define custom command line options using the pytest_addoption hook function
def pytest_addoption(parser):
parser.addoption('--myoption', action='store', default='default value', help='my custom option')
def test_custom_option(request):
assert request.config.getoption('--myoption') == 'custom value'
The above example uses the pytest_addoption
hook function to define the --myoption
option. This option is set to 'default value'
as default value. Also, in the test_custom_option
function, the -request.config.getoption('--myoption')
is used to get the value of the --myoption
option.
For example, you can run the test with the --myoption
option as follows.
$ pytest --myoption custom value
In the above example, 'custom value'
is set as the value of the --myoption
option.
Use the pytest_collection_modifyitems hook function to modify the list of test items
def pytest_collection_modifyitems(items):
for item in items:
if item.parent.name == 'test_file.py':
item.add_marker(pytest.mark.slow)
import time
def test_slow():
time.sleep(5)
def test_fast():
pass
The above example uses the pytest_collection_modifyitems
hook function to add the @pytest.mark.slow
marker to the test items belonging to the file test_file.py
. This allows you to run only those test items with the @slow
marker at test runtime.
For example, you can run only tests with @slow
markers as follows.
$ pytest -m slow
Use pytest_configure hook function to load custom settings from pytest.ini file
def pytest_configure(config):
custom_setting = config.getini('custom_setting')
print(f"custom_setting: {custom_setting}")
[pytest]
custom_setting = True
The above example uses the pytest_configure
hook function to read a custom setting called custom_setting
from the pytest.ini
file. The config.getini()
method is used to read custom settings from the pytest.ini
file.
For example, you can read the pytest.ini
file and use the custom settings as follows.
$ pytest
In the above example, simply running the pytest command reads the custom setting custom_setting
from the pytest.ini
file and displays it in the pytest_configure
hook function.
References