pytilpack.pytest¶
必要なextra
pip install pytilpack[pytest]
pytilpack.pytest
¶
pytest用のユーティリティ集。
AssertBlock(data, suffix='.html', encoding='utf-8', errors='replace', tmp_path=None)
¶
大きいデータ (画面のHTML等) をpytestのassertで確認するためのユーティリティ。
ブロック内でエラーが発生した場合、dataを一時ファイルへ保存し、そのパスを例外メッセージに含める。
例::
def test_something():
data = ... # 画面のHTMLなどの大きいデータ
with pytilpack.pytest.AssertBlock(data, ".html"):
assert "expected string" in data
ソースコード位置: pytilpack/pytest.py
__enter__()
¶
__exit__(exc_type, exc_val, exc_tb)
¶
コンテキストマネージャーのexit。エラーが発生した場合はdataを一時ファイルへ保存し、パスをログと例外メッセージへ含める。
ソースコード位置: pytilpack/pytest.py
__aenter__()
async
¶
__aexit__(exc_type, exc_val, exc_tb)
async
¶
create_temp_view(tmp_path, data, suffix, encoding='utf-8', errors='replace')
¶
データの確認用に一時ファイルを作成する。
ソースコード位置: pytilpack/pytest.py
tmp_file_path(tmp_path=None, suffix='.txt', prefix='tmp')
¶
一時ファイルパスを返す。
ソースコード位置: pytilpack/pytest.py
register_basetmp(tmp_path_factory)
¶
pytestのbasetmpを環境変数に登録する。
get_tmp_pathがシンボリックリンク (pytest-current) 経由のフォールバックを
使わず、pytest本体のbasetmpを直接参照できるようにする。これにより複数の
pytestプロセスが近接した環境でもtmp_pathとの照合が非決定的にならない。
本ライブラリを利用するプロジェクトのtests/conftest.pyにautouseのセッション
スコープfixtureを定義して呼び出すこと。
Example
tests/conftest.pyで以下のように使う::
import pytest
import pytilpack.pytest
@pytest.fixture(autouse=True, scope="session")
def _pytilpack_basetmp(
tmp_path_factory: pytest.TempPathFactory,
) -> None:
pytilpack.pytest.register_basetmp(tmp_path_factory)
ソースコード位置: pytilpack/pytest.py
get_tmp_path()
¶
一時ファイルを置けるディレクトリを返す。
tmp_path/tmp_path_factory fixtureを直接使用することが望ましいが、
手軽に利用したい場合向けの簡易関数である。
register_basetmpが呼ばれていれば、pytest本体のbasetmpを直接返す。
呼ばれていない場合はpytest-of-<user>/pytest-currentシンボリックリンク
経由のフォールバックを使う。ただしフォールバック経路は並列/近接した別の
pytestプロセスがある環境でtmp_pathと一致しないことがあるため、
本ライブラリの利用者はregister_basetmpの利用を推奨する。