2017-06-01 53 views
1

我有一個非常簡單的測試方法:蟒蛇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 =========================== 
+0

報告完整[追蹤](https://docs.python.org/3/library/traceback.html),以提高你獲得答案的機會。 – Kanak

+0

一些更多的信息會很有用 - 例如什麼是「主」? – rammelmueller

+0

@LukasR該測試位於Urbansearch/tests/test_main.py。該方法位於Urbansearch/urbansearch/main.py中。 Main沒有被聲明爲主對象。 p.s.我可以在這裏更好地回覆您的評論,或者編輯主要問題? –

回答