【发布时间】:2022-01-08 16:05:34
【问题描述】:
我们有通过 Pytest 运行的单元测试,它使用自定义装饰器在每次测试之前启动上下文管理的模拟回显服务器,并将其地址作为额外参数提供给测试。这适用于 Python 2。
但是,如果我们尝试在 Python 3 上运行它们,那么 Pytest 会抱怨它找不到与额外参数名称匹配的夹具,并且测试会失败。
我们的测试看起来与此类似:
@with_mock_url('?status=404&content=test&content-type=csv')
def test_file_not_found(self, url):
res_id = self._test_resource(url)['id']
result = update_resource(None, res_id)
assert not result, result
self.assert_archival_error('Server reported status error: 404 Not Found', res_id)
使用这样的装饰器函数:
from functools import wraps
def with_mock_url(url=''):
"""
Start a MockEchoTestServer and call the decorated function with the server's address prepended to ``url``.
"""
def decorator(func):
@wraps(func)
def decorated(*args, **kwargs):
with MockEchoTestServer().serve() as serveraddr:
return func(*(args + ('%s/%s' % (serveraddr, url),)), **kwargs)
return decorated
return decorator
在 Python 2 上这有效;模拟服务器启动,测试获取类似于“http://localhost:1234/?status=404&content=test&content-type=csv”的 URL,然后模拟关闭。
然而,在 Python 3 上,我们得到一个错误,“fixture 'url' not found”。
有没有办法告诉 Python,“这个参数是从其他地方提供的,不需要固定装置”?或者是否有一种简单的方法可以将其变成固定装置?
【问题讨论】:
-
您可以尝试使用
request夹具stackoverflow.com/questions/69852075/… -
感谢您的提示,但这似乎是关于自定义固定装置的创建。在这种情况下,创建参数很容易;困难在于将其作为不是夹具的函数参数传递给测试。
标签: python python-3.x pytest python-decorators fixtures