Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python AttributeError assert_called

I have a very easy test:

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()

which tests the following method:

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()

when testing this I get the error: AttributeError: assert_called. Why do I get this error and how do I solve this? I have also tried many different variations but I always get more or less the same result.

edit: The test is located in Urbansearch/tests/test_main.py. The method is located in Urbansearch/urbansearch/main.py.

The exact output I get is the following;

============================= 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 ===========================
like image 363
Tom Brunner Avatar asked Jun 01 '17 10:06

Tom Brunner


1 Answers

You're using Python 3.5 and the method assert_called is new in Python 3.6.

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

like image 140
AndrewS Avatar answered Nov 10 '22 02:11

AndrewS