py.test 在自定义 funcargs 中使用 Monkeypatch
我使用 py.test 并且非常喜欢 funcarg 方法将对象注入到测试函数中。 在我的测试中,我需要使用模拟对象,因为我有很多外部依赖项。我使用 Monkeypatch 将某些属性替换为模拟对象。
我遇到的问题是,我经常会进行一堆使用特定 funcarg 的测试,并且总是需要修补相同的属性。到目前为止,我替换了每个测试函数中的属性。
有没有办法在我的 funcarg 函数中使用 Monkeypatch,并从各个测试中删除这些重复的代码?
import sys
import pytest
from mock import Mock
#----------------------------------------------------------------------
def pytest_funcarg__api(request):
""""""
api = myclass()
#do some initialisation...
return api
#----------------------------------------------------------------------
def test_bla1(monkeypatch, api):
""""""
monkeypatch.setattr(api,"get_external_stuff",Mock())
monkeypatch.setattr(api,"morestuff",Mock())
api.do_something1()
assert not api.a
#----------------------------------------------------------------------
def test_bla2(monkeypatch, api):
""""""
monkeypatch.setattr(api,"get_external_stuff",Mock())
monkeypatch.setattr(api,"morestuff",Mock())
api.do_something2()
assert api.b
if __name__=='__main__':
pytest.main(args=["-v",sys.argv[0]])
I use py.test and really like the funcarg approach to inject objects into test functions.
In my testing I need to work with Mock objects, as I have a lot external dependencies. I use monkeypatch to replace certain attributes with the mock objects.
The problem I have is, that I will often end up with a bunch of tests that will use a certain funcarg and always require the same attributes patched. So far I replace the attributes in every test function.
Is there a way to use monkeypatch in my funcarg functions, and remove this duplicated code from the individual tests?
import sys
import pytest
from mock import Mock
#----------------------------------------------------------------------
def pytest_funcarg__api(request):
""""""
api = myclass()
#do some initialisation...
return api
#----------------------------------------------------------------------
def test_bla1(monkeypatch, api):
""""""
monkeypatch.setattr(api,"get_external_stuff",Mock())
monkeypatch.setattr(api,"morestuff",Mock())
api.do_something1()
assert not api.a
#----------------------------------------------------------------------
def test_bla2(monkeypatch, api):
""""""
monkeypatch.setattr(api,"get_external_stuff",Mock())
monkeypatch.setattr(api,"morestuff",Mock())
api.do_something2()
assert api.b
if __name__=='__main__':
pytest.main(args=["-v",sys.argv[0]])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用记录的 getfuncargvalue 函数在内部使用来自另一个函数参数工厂的函数参数:
You can use the documented getfuncargvalue function to internally use a function argument from another function argument's factory:
这应该可行:
这里的技巧有两个:
config.pluginmanager
获取monkeypatch
插件。pytest_funcarg__monkeypatch()
funcarg 接口来欺骗monkeypatch
插件,使其认为它是由 py.test 的依赖注入代码调用的。This should work:
The trick here is two-fold:
monkeypatch
plugin usingconfig.pluginmanager
.monkeypatch
plugin into thinking its called by py.test's dependency injection code, by calling itspytest_funcarg__monkeypatch()
funcarg-interface with our very own request object.