繁体   English   中英

用于内省调用函数的 pytest 固定装置

[英]pytest fixture to introspect calling function

我有一个测试类和一个设置函数,如下所示:

@pytest.fixture(autouse=True, scope='function')
def setup(self, request):
    self.client = MyClass()
    first_patcher = patch('myclass.myclass.function_to_patch')
    first_mock = first_patcher.start()
    first_mock.return_value = 'foo'
    value_to_return = getattr(request, 'value_name', None)
    second_patcher = patch('myclass.myclass.function_two')
    second_mock = second_patcher.start()
    second_mock.return_value = value_to_return
    #could clean up my mocks here, but don't care right now

我在 pytest 的文档中看到,可以对模块级值进行内省: val = getattr(request.module, 'val_name', None)

但是,我希望能够根据我所在的测试指定不同的返回值。所以我正在寻找一种方法来内省 test_function 而不是 test_module。

http://pytest.org/latest/fixture.html#fixtures-can-introspect-the-requesting-test-context

您可以使用request.function来获取测试功能。 只需按照您引用的b wepage上的链接查看测试request对象上的可用内容:)

也许自接受答案以来文档已更改。 至少对我来说不清楚如何

只需点击链接

所以我想我会用链接本身更新这个线程:

https://pytest.org/en/6.2.x/reference.html#request

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM