python AttributeError assert_called



我有一个非常简单的测试:

from unittest.mock import Mock
from urbansearch import main
main.ArgumentParser = Mock()
def test_parse_arguments():
    main.parse_arguments()
    main.ArgumentParser.add_argument.assert_called()

测试以下方法:

from argparse import ArgumentParser
def parse_arguments():
    parser = ArgumentParser(description='The TU Delft Urbansearch CLI')
    parser.add_argument('-d', '--directory',
                    help='Source files directory containing files with '
                          + 'indices')
    return parser.parse_args()

测试此问题时,我会收到错误: attributeError:assert_called 。为什么我会得到这个错误,该如何解决?我也尝试了许多不同的变体,但我总是或多或少都会得到相同的结果。

编辑:该测试位于urbansearch/tests/test_main.py中。该方法位于urbansearch/urbansearch/main.py。

我得到的确切输出是以下;

============================= test session starts =============================
platform win32 -- Python 3.5.3, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: C:Userstom_bOneDriveDokumenteGitHubUrbanSearch, inifile:
plugins: cov-2.3.1
collected 4 items
test_main.py FFFF
================================== FAILURES ===================================
___________________________ test_selection_workers ____________________________
    def test_selection_workers():
>       assert False
E       assert False
test_main.py:10: AssertionError
________________________ test_download_indices_for_url ________________________
    def test_download_indices_for_url():
>       assert False
E       assert False
test_main.py:14: AssertionError
____________________ test_classify_documents_from_indices _____________________
    def test_classify_documents_from_indices():
>       assert False
E       assert False
test_main.py:18: AssertionError
____________________________ test_parse_arguments _____________________________
    def test_parse_arguments():
        main.parse_arguments()
>       main.ArgumentParser.add_argument.assert_called()
test_main.py:23:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <Mock name='mock.add_argument' id='2022243875864'>
name = 'assert_called'
    def __getattr__(self, name):
        if name in {'_mock_methods', '_mock_unsafe'}:
            raise AttributeError(name)
        elif self._mock_methods is not None:
            if name not in self._mock_methods or name in _all_magics:
                raise AttributeError("Mock object has no attribute %r" % name)
        elif _is_magic(name):
            raise AttributeError(name)
        if not self._mock_unsafe:
            if name.startswith(('assert', 'assret')):
>               raise AttributeError(name)
E               AttributeError: assert_called
C:Userstom_bAnaconda3libunittestmock.py:585: AttributeError
========================== 4 failed in 1.68 seconds ===========================

您使用的是python 3.5,方法assert_called在Python 3.6中是新的。

https://docs.python.org/release/3.6.0/library/unittest.mock.html#unittest.mock.mock.mock.mock.assert_called

相关内容

  • 没有找到相关文章

最新更新