簡體   English   中英

python AttributeError assert_called

[英]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:\Users\tom_b\OneDrive\Dokumente\GitHub\UrbanSearch, 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:\Users\tom_b\Anaconda3\lib\unittest\mock.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.assert_called

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM