【发布时间】:2019-02-05 19:02:21
【问题描述】:
在我的单元测试中,我有两个非常相似的装置,我希望将一些功能分解为某种辅助函数。鉴于我对yield 如何生成生成器的理解,我认为这不会引起任何问题。 my_fixture_with_helper,应该只返回 fixture_helper 生成的生成器。
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
但是,如果我运行 pytest --capture=no,我会得到以下信息
test_foo.py Initialized from the fixture...
.Tearing down after the fixture...
.
我希望“Initialized from the helper”和“Tearing down after the helper”会被打印出来,但事实并非如此,我不知道为什么。为什么这不起作用?
【问题讨论】: