我在一个文件夹中有两个python单元测试文件
test/folder1/test_file1.py
test/folder2/test_file2.py
当我单独运行测试时,它们通过,但当我在目录级别运行时,第一个测试文件通过,但第二个测试文件失败。为什么会发生这种情况?两个测试文件都导入了一些相同的模块。一个文件使用MagicMock,另一个不使用
Pass commands
python3 -m pytest test/folder1/test_file1.py
python3 -m pytest test/folder1/test_file2.py
Fail command
python3 -m pytest test/folder1/
在测试文件1中导入命令
import os
import shutil
import tempfile
import xlrd
from unittest import TestCase
from example.folder1 import module1
from example.folder4 import module4
在测试文件2中导入命令
import xlrd
from unittest import TestCase
from unittest.mock import MagicMock
from example.folder1 import module1 (same as test_file1)
from example.folder2 import module2
from example.folder3 import module3
import pkg_resources
发现了问题
我们使用了一个本地缓存变量。第一个测试是设置缓存,第二个测试使用第一个测试中的缓存
在运行第二个测试之前清除setupClass()中的缓存解决了该问题
相关问题 更多 >
编程相关推荐