Noests:在多个文件上运行nosetests时,使用@patch使用@patch



我正在跨多个文件运行nosetests,并遇到与特定文件导入有关的错误,好吧,我实际上不确定该错误与什么相关,我认为这是随着其修补的进口或某些内容。错误本身看起来像:

(我为使用@Patch Decorator的每个测试功能遇到了其中一个错误)

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测试类本身上运行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"
]        

最新更新