用于内省调用函数的pytest fixture

2024-06-16 14:06:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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

@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=获取属性(请求模块,'val'u name',无)

但是,我希望能够根据我所处的测试指定要返回的不同值。所以我在寻找一种方法来反省test_函数而不是test_模块。在

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


Tags: 模块to函数testreturnpytestvaluemyclass