lazy_fixture の例 ― 2023年05月21日 12時40分54秒
まず、パッケージをインストール。
% pip install pytest.lazy_fixture
parametrize は number という、fixture を取る。number の fixture は lazy_fixture を通して、one と two のそれぞれを取る。これらの fixture は業と assert があり、両方をテストから並べてしまうと、副作用により二つ目の fixture が呼べなくなる。そこで、lazy_fixture を用いて、二回のテストで、各々一つずつの呼び出す。
% cat test_lazy.py
import pytest
@pytest.fixture
def one():
assert False, "one"
@pytest.fixture
def two():
assert False, "two"
@pytest.mark.parametrize(
"number", [(pytest.lazy_fixture("one")), (pytest.lazy_fixture("two"))]
)
def test_lazy(number):
pass
そして、pytest を走らせる。各々の fixture が目的の assert を呼んでいる。
[venv38] % pytest
============================= test session starts ==============================
platform freebsd12 -- Python 3.8.15, pytest-7.1.2, pluggy-1.0.0
rootdir: /export/home/uyota/pytest-test
plugins: cov-3.0.0, lazy-fixture-0.6.3
collected 2 items
test_lazy.py EE [100%]
==================================== ERRORS ====================================
_______________________ ERROR at setup of test_lazy[one] _______________________
@pytest.fixture
def one():
> assert False, "one"
E AssertionError: one
E assert False
test_lazy.py:6: AssertionError
_______________________ ERROR at setup of test_lazy[two] _______________________
@pytest.fixture
def two():
> assert False, "two"
E AssertionError: two
E assert False
test_lazy.py:11: AssertionError
=========================== short test summary info ============================
ERROR test_lazy.py::test_lazy[one] - AssertionError: one
ERROR test_lazy.py::test_lazy[two] - AssertionError: two
============================== 2 errors in 0.05s ==============================
前回。
コメント
トラックバック
このエントリのトラックバックURL: http://uyota.asablo.jp/blog/2023/05/21/9587908/tb
※なお、送られたトラックバックはブログの管理者が確認するまで公開されません。
コメントをどうぞ
※メールアドレスとURLの入力は必須ではありません。 入力されたメールアドレスは記事に反映されず、ブログの管理者のみが参照できます。
※なお、送られたコメントはブログの管理者が確認するまで公開されません。