Traffine I/O

日本語

2023-02-17

pytestのconftest.py

conftest.py

pytestのconftest.pyは、pytestがテストコレクション、テストの実行、および結果のレポートを行う際に使用する設定ファイルです。conftest.pyは、pytestの実行中に自動的に検出され、テストコレクションおよび実行中にpytestが使用するfixture、プラグイン、コマンドラインオプション、およびカスタマイズされたテストランナーを定義できます。

conftest.pyは、テストファイルと同じディレクトリに置くことができます。また、テストファイルがあるディレクトリの親ディレクトリに置くことで、複数のテストファイルで共有されるfixtureを定義することもできます。

以下は、conftest.pyでfixtureを定義する例です。

conftest.py
import pytest

@pytest.fixture
def my_fixture():
    return 'Hello, pytest!'

このように、conftest.py内で@pytest.fixtureデコレータを使用してfixtureを定義することができます。上記の例では、my_fixtureという名前のfixtureを定義しています。

複数のテストファイルで共有される fixture を定義

以下は、conftest.pyを使用して、複数のテストファイルで共有されるfixtureを定義する例です。

conftest.py
import pytest

@pytest.fixture
def my_fixture():
return 'Hello, pytest!'
test_file1.py
def test_my_fixture(my_fixture):
assert my_fixture == 'Hello, pytest!'
test_file2.py
def test_my_fixture(my_fixture):
assert my_fixture == 'Hello, pytest!'

上記の例では、conftest.pyに定義されたmy_fixtureという名前のfixtureを、test_file1.pyおよびtest_file2.pyで使用しています。これにより、複数のテストファイルで共有されるfixtureを定義することができます。

conftest.py と pytest フック関数

以下は、conftest.pyとpytestフック関数を使用した例です。

pytest_addoption フック関数を使用して、カスタムコマンドラインオプションを定義

conftest.py
def pytest_addoption(parser):
    parser.addoption('--myoption', action='store', default='default value', help='my custom option')
test_file.py
def test_custom_option(request):
    assert request.config.getoption('--myoption') == 'custom value'

上記の例では、pytest_addoptionフック関数を使用して、--myoptionオプションを定義しています。このオプションには、デフォルト値として'default value'が設定されています。また、test_custom_option関数で、request.config.getoption('--myoption')を使用して、--myoptionオプションの値を取得しています。

例えば、以下のようにして--myoptionオプションを指定してテストを実行することができます。

bash
$ pytest --myoption custom value

上記の例では、--myoptionオプションの値として'custom value'が設定されています。

pytest_collection_modifyitems フック関数を使用して、テストアイテムのリストを変更

conftest.py
def pytest_collection_modifyitems(items):
    for item in items:
        if item.parent.name == 'test_file.py':
            item.add_marker(pytest.mark.slow)
test_file.py
import time

def test_slow():
    time.sleep(5)

def test_fast():
    pass

上記の例では、pytest_collection_modifyitemsフック関数を使用して、test_file.pyというファイルに属するテストアイテムに、@pytest.mark.slowマーカーを追加しています。これにより、テスト実行時に@slowマーカーが付けられたテストアイテムだけを実行することができます。

例えば、以下のようにして@slowマーカーが付けられたテストのみを実行することができます。

bash
$ pytest -m slow

pytest_configure フック関数を使用して、pytest.ini ファイルからカスタム設定をロード

conftest.py
def pytest_configure(config):
custom_setting = config.getini('custom_setting')
print(f"custom_setting: {custom_setting}")
pytest.ini
[pytest]
custom_setting = True

上記の例では、pytest_configureフック関数を使用して、pytest.iniファイルからcustom_settingというカスタム設定を読み込んでいます。 config.getini()メソッドを使用して、pytest.iniファイルからカスタム設定を読み込んでいます。

例えば、以下のようにしてpytest.iniファイルを読み込み、カスタム設定を使用することができます。

bash
$ pytest

上記の例では、pytestコマンドを実行するだけで、pytest.iniファイルからcustom_settingというカスタム設定を読み込んで、 pytest_configureフック関数で表示しています。

参考

https://docs.pytest.org/en/7.2.x/
https://github.com/pytest-dev/pytest

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!