在这个单元测试中,我希望确保Django自定义管理任务正在向其日志中写入特定的消息。
from unittest.mock import Mock, patch
...
@override_settings(HR_SYNC_DATA_DIR=f"{settings.BASE_DIR}/core/management/tests/HR_SYNC_DATA_DIR")
@patch('core.management.commands.process_hr_changes.log')
@patch('core.management.commands.process_hr_changes.requests.get')
def test_sync_users2(self, mock_get, mock_log):
#
# Load our Mock data
#
test_data_dir = f"{settings.BASE_DIR}/core/management/tests/TEST_DATA_DIR"
get_mocks = []
for filename in glob.glob(path.join(test_data_dir, 'test-data-hrdata_underrun_page_*.json')):
with open(filename, "r") as infile:
mock_response = Mock(ok=True)
mock_response.json.return_value = json.load(infile)
mock_response.status_code = status.HTTP_200_OK
get_mocks.append(mock_response)
mock_get.side_effect = get_mocks
with self.assertRaises(CommandError) as context:
call_command('process_hr_changes')
print(context.exception)
self.assertEqual('percent change is -0.125 and delta_pct is 0.1. Lifecycle is dev.', context.exception.args[0])
mock = Mock(return_value=None)
info_calls = [
mock.call('rehires = 0 new hires = 0 terminations = 5.'),
mock.call('process_hr_changes args are: delta_pct 0.1 how_many_hours_back 24 dry_run False debug is False verbose is False'),
mock.call('test: startup'),
mock.call('Removing directory /apps/core/management/tests/HR_SYNC_DATA_DIR.'),
mock.call('Creating directory /apps/core/management/tests/HR_SYNC_DATA_DIR.'),
mock.call('percent change is -0.125 and delta_pct is 0.1.'),
]
for mock_call in mock_log.info.mock_calls:
print(mock_call)
mock_log.info.assert_has_calls(info_calls, any_order=True)
当我运行此测试时,它会在带有以下消息的mock_log.info.assert_has_calls(info_calls, any_order=True)
语句上失败:
AssertionError: (<MagicMock name='log.call()' id='140711932625744'>, <MagicMock name='log.call()' id='140711932625744'>, <MagicMock name='log.call()' id='140711932625744'>, <MagicMock name='log.call()' id='140711932625744'>, <MagicMock name='log.call()' id='140711932625744'>, <MagicMock name='log.call()' id='140711932625744'>) not all found in call list
我还看到了我的打印语句的输出:
call('process_hr_changes args are: delta_pct 0.1 how_many_hours_back 24 dry_run False debug is False verbose is False')
call('test: startup')
call('Removing directory /apps/core/management/tests/HR_SYNC_DATA_DIR.')
call('Creating directory /apps/core/management/tests/HR_SYNC_DATA_DIR.')
call('rehires = 0 new hires = 0 terminations = 5.')
call('percent change is -0.125 and delta_pct is 0.1.')
我做错了什么?
谢谢
更新:
我不得不把我的代码改为:
from unittest.mock import Mock, patch, call # need to import call.
...
info_calls = [
call('rehires = 0 new hires = 0 terminations = 5.'),
call('process_hr_changes args are: delta_pct 0.1 how_many_hours_back 24 dry_run False debug is False verbose is False'),
call('test: startup'),
call('Removing directory /apps/core/management/tests/HR_SYNC_DATA_DIR.'),
call('Creating directory /apps/core/management/tests/HR_SYNC_DATA_DIR.'),
call('percent change is -0.125 and delta_pct is 0.1.'),
]
mock_log.info.assert_has_calls(info_calls, any_order=True)
发布于 2020-08-03 19:20:11
您正在将Mock.call
的结果传递给assert_has_calls
。但是,assert_has_calls
需要一个顶级的call
对象。
注意,在阅读了文档之后,我同意这一点并不是很清楚。这是assert_has_calls
的文档
断言已使用指定的调用调用了模拟。将检查mock_calls列表中的调用。
如果any_order是假的,那么调用必须是顺序的。可以在指定的呼叫之前或之后进行额外的呼叫。
如果any_order是真的,那么调用可以是任意顺序的,但是它们都必须出现在mock_calls中。
模拟(return_value=None)模拟(1)模拟(2)模拟(3)模拟(4)调用=调用(2),调用(3)mock.assert_has_calls(调用)调用=调用(4),调用(2),调用(3)mock.assert_has_calls(调用,any_order=True)
在该示例中,还不清楚call
对象来自何处。我能看到混乱。
只有当您看到call
文档时,它才变得更加清晰(强调我的):
call()是一个帮助对象,用于进行简单的断言,与call_args、call_args_list、mock_calls和method_calls进行比较。assert_has_calls().
也可以使用调用()
https://stackoverflow.com/questions/63235007
复制相似问题