def test_without_required_parameters(capfd, patch_xfconf): """ Failure must occurs when all parameters are missing """ with pytest.raises(SystemExit): xfconf.main() out, err = capfd.readouterr() results = json.loads(out) assert results['failed'] assert 'missing required arguments' in results['msg']
def test_xfconf(mocker, capfd, patch_xfconf, testcase): """ Run unit tests for test cases listen in TEST_CASES """ # Mock function used for running commands first call_results = [item[2] for item in testcase['run_command.calls']] mock_run_command = mocker.patch( 'ansible_collections.community.general.plugins.module_utils.mh.module_helper.AnsibleModule.run_command', side_effect=call_results) # Try to run test case with pytest.raises(SystemExit): xfconf.main() out, err = capfd.readouterr() results = json.loads(out) print("testcase =\n%s" % testcase) print("results =\n%s" % results) assert 'changed' in results assert results['changed'] == testcase['changed'] for test_result in ('channel', 'property'): assert test_result in results, "'{0}' not found in {1}".format( test_result, results) assert results[test_result] == results['invocation']['module_args'][test_result], \ "'{0}': '{1}' != '{2}'".format(test_result, results[test_result], results['invocation']['module_args'][test_result]) for conditional_test_result in ('msg', 'value', 'previous_value'): if conditional_test_result in testcase: assert conditional_test_result in results, "'{0}' not found in {1}".format( conditional_test_result, results) assert results[conditional_test_result] == testcase[conditional_test_result], \ "'{0}': '{1}' != '{2}'".format(conditional_test_result, results[conditional_test_result], testcase[conditional_test_result]) assert mock_run_command.call_count == len(testcase['run_command.calls']) if mock_run_command.call_count: call_args_list = [(item[0][0], item[1]) for item in mock_run_command.call_args_list] expected_call_args_list = [(item[0], item[1]) for item in testcase['run_command.calls']] print("call args list =\n%s" % call_args_list) print("expected args list =\n%s" % expected_call_args_list) assert call_args_list == expected_call_args_list
def test_xfconf(mocker, capfd, patch_xfconf, testcase): """ Run unit tests for test cases listen in TEST_CASES """ # Mock function used for running commands first call_results = [item[2] for item in testcase['run_command.calls']] mock_run_command = mocker.patch( 'ansible_collections.community.general.plugins.module_utils.module_helper.AnsibleModule.run_command', side_effect=call_results) # Try to run test case with pytest.raises(SystemExit): xfconf.main() out, err = capfd.readouterr() results = json.loads(out) print("testcase =\n%s" % testcase) print("results =\n%s" % results) assert 'changed' in results assert results['changed'] == testcase['changed'] if 'msg' in results: assert results.get('msg') == testcase['msg'] if 'value' in results: assert results['value'] == testcase['value'] if 'previous_value' in results: assert results['previous_value'] == testcase['previous_value'] assert mock_run_command.call_count == len(testcase['run_command.calls']) if mock_run_command.call_count: call_args_list = [(item[0][0], item[1]) for item in mock_run_command.call_args_list] expected_call_args_list = [(item[0], item[1]) for item in testcase['run_command.calls']] print("call args list =\n%s" % call_args_list) print("expected args list =\n%s" % expected_call_args_list) assert call_args_list == expected_call_args_list