Calling function that yields from a pytest fixture
在我的单元测试中,我有两个非常相似的装置,我希望将一些功能分解成某种辅助功能。鉴于我对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import pytest def fixture_helper(): print("Initialized from the helper...") yield 26 print("Tearing down after the helper...") @pytest.fixture def my_fixture_with_helper(): return fixture_helper() @pytest.fixture def my_fixture(): print("Initialized from the fixture...") yield 26 print("Tearing down after the fixture...") def test_nohelper(my_fixture): pass def test_helper(my_fixture_with_helper): pass |
但是,如果我运行
1 2 3 | test_foo.py Initialized from the fixture... .Tearing down after the fixture... . |
我希望"从助手初始化"和"在助手之后拆掉"都能打印出来,但事实并非如此,我也不知道为什么。为什么这不起作用?
型
您需要使用
1 2 3 | @pytest.fixture def my_fixture_with_helper(): yield from fixture_helper() |
更多关于