要在不同的测试文件中独立导入同一模块的 pytest,可以按照以下步骤进行解决:
conftest.py
的文件,并将需要共享的模块导入其中。# conftest.py
import pytest
# 共享模块的导入
from my_module import my_function
# test_file1.py
import pytest
def test_function():
# 使用共享模块中的函数
result = my_function()
assert result == expected_result
# test_file2.py
import pytest
def test_another_function():
# 使用共享模块中的函数
result = my_function()
assert result == expected_result
conftest.py
文件,并使共享模块可用于所有测试文件。请确保 conftest.py
文件位于包含测试文件的同一目录下,并且模块的导入路径正确。
上一篇:不同测试类中常用的注解
下一篇:不同测试用例之间的打印格式不同