我有一个测试类和一个设置函数,如下所示:
@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
您可以使用
request.function
来获取测试函数。只需按照您引用的b wepage上的链接查看testrequest
对象上有哪些可用的内容:)相关问题 更多 >
编程相关推荐