[英]Running multiple tests cause interference in nosetests when patching with @mock.patch.object and sometimes also when using `with mock.patch.object`
[英]Noestests: AttributeError using @patch when running Nosetests on multiple files
我正在多个文件上进行鼻子测试,并收到与导入特定文件有关的错误,我实际上不确定该错误与什么相关,我认为这与导入有关或与修补有关它的。 错误本身看起来像:
(对于使用@patch装饰器的每个测试函数,我都会收到这些错误之一)
Error
Traceback (most recent call last):
File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/unittest2/case.py", line 67, in testPartExecutor
yield
File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/unittest2/case.py", line 625, in run
testMethod()
File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/mock/mock.py", line 1297, in patched
arg = patching.__enter__()
File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/mock/mock.py", line 1353, in __enter__
self.target = self.getter()
File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/mock/mock.py", line 1523, in <lambda>
getter = lambda: _importer(target)
File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/mock/mock.py", line 1210, in _importer
thing = _dot_lookup(thing, comp, import_path)
File "/home/user/Documents/venvs/migration/local/lib/python2.7/site-packages/mock/mock.py", line 1200, in _dot_lookup
return getattr(thing, comp)
AttributeError: 'module' object has no attribute 'utils'
包结构如下所示:
my_package
- my_module
- __init__.py
- utils.py
- other.py
- tests
- test_utils.py
- test_other.py
鼻子测试命令:
nosetests -e unit --with-coverage --cover-package=my_package --cover-erase --cover-xml --with-xunit tests --nocapture
因此,很奇怪的是,如果我仅对utils测试类本身运行过鼻子测试,则可以正常运行,所有导入工作且所有补丁均有效,没有错误,所有测试均通过。
这是test_utils.py文件的样子:
from my_module.utils import *
class TestBusinessProcess(unittest2.TestCase):
@patch('my_module.utils.something')
def test_some_utils_function(self, something_mock):
# test implementation..
# this function will throw:
# AttributeError: 'module' object has no attribute 'utils'
# when running whole tests folder and not on individual test file
pass
@patch('my_module.utils.something_else')
def test_some_other_utils_function(self, something_else_mock):
# test implementation..
# same as above
pass
另一个测试文件中的一个测试示例,无论哪种运行方式都没有问题:
from my_module.other import *
class TestBusinessProcess(unittest2.TestCase):
@patch('my_module.other.something')
def test_some_function(self, something_mock):
# test implementation..
# no issues!
pass
@patch('my_module.other.something_else')
def test_some_other_function(self, something_else_mock):
# test implementation..
# no issues!
pass
任何帮助,不胜感激。
我仍然不知道出了什么问题,但这似乎与导入有关。
无论如何,此解决方法可以解决问题,但不确定为什么会这样。
my_module
中的__init__.py
最初为空。 然后,我对其进行了编辑,以显示utils.py
的各个功能:
__init__.py
from utils import test_some_utils_function, test_some_other_utils_function
__all__ = [
"test_some_utils_function",
"test_some_other_utils_function"
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.