【问题标题】:Noestests: AttributeError using @patch when running Nosetests on multiple filesNoestests:在多个文件上运行 Nosetests 时使用 @patch 的 AttributeError
【发布时间】:2016-10-16 14:13:42
【问题描述】:

我正在跨多个文件运行鼻子测试,并收到与导入特定文件有关的错误,我实际上并不确定该错误与什么有关,我认为它要么与导入有关,要么与某些问题有关随着它的修补。错误本身看起来像:

(对于每个使用 @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

非常感谢任何帮助。

【问题讨论】:

    标签: python unit-testing testing nose


    【解决方案1】:

    我仍然不知道出了什么问题,但它似乎与导入有关。

    无论如何,这个解决方法解决了这个问题,但不知道究竟是为什么。

    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"
    ]        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多