pytest fixture对内省调用函数

Mar*_*ark 3 python introspection fixtures pytest

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

@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
Run Code Online (Sandbox Code Playgroud)

我在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

hpk*_*k42 5

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