在我的单元测试中,我有两个非常相似的fixture,我希望将一些功能分解成某种帮助函数。根据我对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”,但它没有打印出来,我也不知道为什么。为什么这不起作用?
发布于 2019-02-06 03:17:14
您需要使用yield from才能正确传递生成器。否则将返回生成器对象,pytest不会将其识别为生成器。
@pytest.fixture
def my_fixture_with_helper():
yield from fixture_helper()有关yield from的更多信息,请访问this stackoverflow post.
https://stackoverflow.com/questions/54541338
复制相似问题