@pytest.mark.hookwrapper获得原函数的参数
时间: 2024-10-16 16:20:41 浏览: 62
`pytest.mark.hookwrapper` 是 Pytest 框架中的一个标记(marker),用于装饰测试套件中的钩子函数(hook functions)。它提供了一种方法让你可以在原始钩子函数执行前后添加额外的行为,同时还能访问到原函数的参数。当你使用 `@pytest.mark.hookwrapper` 装饰一个函数时,这个函数会在目标钩子函数运行时作为包装器,允许你在其中获取、保存甚至修改钩子函数接收到的参数。
例如,如果你想记录某个钩子函数的输入参数,你可以这样做:
```python
import pytest
def wrapper(func):
def wrapper_function(*args, **kwargs):
original_args = args
original_kwargs = kwargs
result = func(*args, **kwargs)
# 这里可以对参数做进一步处理
print(f"Original arguments: {original_args}, {original_kwargs}")
return result
return wrapper_function
@pytest.hookwrapper
def hook_function(original_func, *args, **kwargs):
wrapped = wrapper(original_func)
return wrapped(*args, **kwargs)
# 现在,当调用 hook_function 时,wrapper 函数会被自动执行,并捕获参数
```
阅读全文
相关推荐


















