可以使用pytest自带的fixture机制来实现将本地导入作为fixture的功能。
首先需要在conftest.py文件中定义一个fixture,使用pytest自带的fixture装饰器,并在fixture函数中进行本地导入。如下所示:
import os
import pytest
@pytest.fixture
def local_import():
from my_module import my_function
return my_function
接下来,在测试函数中可以将该fixture作为参数传入,并调用它返回的本地导入的函数。例如:
def test_something(local_import):
assert local_import(arg1, arg2) == expected_result
这样就可以在测试函数中直接使用本地导入的函数了。注意,本地导入的模块需要在测试文件所在目录下或者在PYTHONPATH中可被找到。
下一篇:本地到远程的代理通过UDP