Mock.assert_has_calls() 未按预期工作
Mock.assert_has_calls() not working as expected
我有这个单元测试,我想确保我的 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)
您正在将 Mock.call
的结果传递给 assert_has_calls
。但是,assert_has_calls
需要一个 top-level call
对象。
请注意,阅读文档后我同意这不是很清楚。这是 assert_has_calls
:
的文档
assert the mock has been called with the specified calls. The mock_calls list is checked for the calls.
If any_order is false then the calls must be sequential. There can be extra calls before or after the specified calls.
If any_order is true then the calls can be in any order, but they must all appear in mock_calls.
mock = Mock(return_value=None)
mock(1)
mock(2)
mock(3)
mock(4)
calls = [call(2), call(3)]
mock.assert_has_calls(calls)
calls = [call(4), call(2), call(3)]
mock.assert_has_calls(calls, any_order=True)
不清楚该示例中的 call
对象来自何处。我可以看到混乱。
只有当您看到 call
文档时,它才会变得更加清晰(强调我的):
call() is a helper object for making simpler assertions, for comparing with call_args, call_args_list, mock_calls and method_calls. call() can also be used with assert_has_calls().
我有这个单元测试,我想确保我的 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)
您正在将 Mock.call
的结果传递给 assert_has_calls
。但是,assert_has_calls
需要一个 top-level call
对象。
请注意,阅读文档后我同意这不是很清楚。这是 assert_has_calls
:
assert the mock has been called with the specified calls. The mock_calls list is checked for the calls.
If any_order is false then the calls must be sequential. There can be extra calls before or after the specified calls.
If any_order is true then the calls can be in any order, but they must all appear in mock_calls.
mock = Mock(return_value=None) mock(1) mock(2) mock(3) mock(4) calls = [call(2), call(3)] mock.assert_has_calls(calls) calls = [call(4), call(2), call(3)] mock.assert_has_calls(calls, any_order=True)
不清楚该示例中的 call
对象来自何处。我可以看到混乱。
只有当您看到 call
文档时,它才会变得更加清晰(强调我的):
call() is a helper object for making simpler assertions, for comparing with call_args, call_args_list, mock_calls and method_calls. call() can also be used with assert_has_calls().