gpt4 book ai didi

python AttributeError assert_called

转载 作者:太空狗 更新时间:2023-10-29 21:33:32 29 4
gpt4 key购买 nike

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

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

关于python AttributeError assert_called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44305030/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com