我已经将pytest更新到了4.3.0,现在我需要重新编写测试代码,因为直接调用fixture已被弃用。
我有一个在unittest.TestCase中使用的fixture的问题,我如何获得从fixture返回的值,而不是对函数本身的引用?
示例:
@pytest.fixture
def test_value():
return 1
@pytest.mark.usefixtures("test_value")
class test_class(unittest.TestCase):
def test_simple_in_class(self):
print(test_value) # prints the function reference and not the value
print(test_value()) # fails with Fixtures are not meant to be called directly
def test_simple(test_value):
print(test_value) # prints 1如何在test_simple_in_class()方法中获取test_value?
发布于 2019-02-26 21:47:59
已经有一个big discussion on this了。你可以通读一下或者参考deprecation documentation。
在你设计的例子中,似乎这就是答案:
@pytest.fixture(name="test_value")
def test_simple_in_class(self):
print(test_value())但是,我建议您查看文档。另一个示例可能就是您想要的。你可以阅读我链接到的讨论,以了解其中的一些原因。不过,这场辩论变得有点激烈。
发布于 2019-02-26 22:34:23
如果有人感兴趣的话,这是我的简单示例的解决方案。
def my_original_fixture():
return 1
@pytest.fixture(name="my_original_fixture")
def my_original_fixture_indirect():
return my_original_fixture()
@pytest.mark.usefixtures("my_original_fixture")
class test_class(unittest.TestCase):
def test_simple_in_class(self):
print(my_original_fixture())
def test_simple(my_original_fixture):
print(my_original_fixture)https://stackoverflow.com/questions/54886692
复制相似问题