简体   繁体   English

在目录级别运行时,Python 3 单元测试失败

[英]Python 3 unit test fails when running at directory level

I have two python unit test files in a folder我在一个文件夹中有两个 python 单元测试文件

test/folder1/test_file1.py
test/folder2/test_file2.py

When I run the tests individually they pass, but when I run at the directory level the first test file passes but the second test file fails.当我单独运行测试时,它们通过,但是当我在目录级别运行时,第一个测试文件通过但第二个测试文件失败。 Why would this happen?为什么会发生这种情况? Both test files import some of the same modules.两个测试文件都导入了一些相同的模块。 One file uses MagicMock the other doesn't一个文件使用 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/

import commands in test_file1在 test_file1 中导入命令

import os
import shutil
import tempfile
import xlrd
from unittest import TestCase

from example.folder1 import module1
from example.folder4 import module4

Import commands in test_file2在 test_file2 中导入命令

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

Found the issue.发现问题。

We were using a local CACHE variable.我们使用的是本地 CACHE 变量。 The first test was setting up the cache, the second test ended up using the cache from the first test.第一个测试是设置缓存,第二个测试最终使用了第一个测试中的缓存。

Clearing the cache in setupClass() before running the second test solved the issue在运行第二个测试之前清除 setupClass() 中的缓存解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM