Noestests:AttributeError 使用@patch when 运行 Nosetests on multiple files
Noestests: AttributeError using @patch when running Nosetests on multiple files
我正在 运行 对多个文件进行 nosetests 并收到与导入特定文件相关的错误,我实际上不确定错误与什么有关,我认为它要么是什么搞定进口或修补它的东西。错误本身看起来像:
(对于使用 @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 命令:
nosetests -e unit --with-coverage --cover-package=my_package --cover-erase --cover-xml --with-xunit tests --nocapture
所以奇怪的是,如果我 运行 仅在 utils 测试 class 本身上进行 nosetests,它 运行 没问题,所有导入工作和所有补丁工作,没有错误, 所有测试都通过。
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"
]
我正在 运行 对多个文件进行 nosetests 并收到与导入特定文件相关的错误,我实际上不确定错误与什么有关,我认为它要么是什么搞定进口或修补它的东西。错误本身看起来像:
(对于使用 @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 命令:
nosetests -e unit --with-coverage --cover-package=my_package --cover-erase --cover-xml --with-xunit tests --nocapture
所以奇怪的是,如果我 运行 仅在 utils 测试 class 本身上进行 nosetests,它 运行 没问题,所有导入工作和所有补丁工作,没有错误, 所有测试都通过。
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"
]