2017-09-17 8 views
-1

내가 만들고있는 일부 코드에서 일부 단위 테스트를 구현하려고하지만이 이상한 동작을보고 있는데 어디에 함수 호출의 반환 값을 설정하더라도 거짓, 관련 코드가 실행되지 않으므로 어설 션 instance.fail_json.assert_called_with(msg='Not enough parameters specified.')이 실패합니다. 어설 션이 파이썬 모크에서 실행되지 않는다

Console Output

내가 설정해야 할 다른 뭔가가 있나요?

project.py :

def main(): 
    # define the available arguments/parameters that a user can pass 
    # to the module 
    module_args = dict(
     name=dict(type='str', required=True), 
     ticktype=dict(type='str'), 
     path=dict(type='str'), 
     dbrp=dict(type='str'), 
     state=dict(type='str', required=True, choices=["present", "absent"]), 
     enable=dict(type='str', default="no", choices=["yes","no","da","net"]) 
    ) 

    required_if=[ 
     [ "state", "present", ["name", "type", "path", "dbrp", "enabled"] ], 
     [ "state", "absent", ["name"]] 
    ] 

    # seed the result dict in the object 
    # we primarily care about changed and state 
    # change is if this module effectively modified the target 
    # state will include any data that you want your module to pass back 
    # for consumption, for exampole, in a subsequent task 
    result = dict(
     changed=False, 
     original_message='', 
     message='' 
    ) 

    # the AnsibleModule object will be our abstraction working with Ansible 
    # this includes instantiation, a couple of common attr would be the 
    # args/params passed to the execution, as well as if the module 
    # supports check mode 
    module = AnsibleModule(
     argument_spec=module_args, 
     supports_check_mode=False 
    ) 

    # if the user is working with this module in only check mode we do not 
    # want to make any changes to the environment, just return the current 
    # state with no modifications 
    if module.check_mode: 
     return result 

    return_val = run_module(module) 
    return_val = True 
    if return_val is True: 
     module.exit_json(changed=True, msg="Project updated.") 
    else: 
     module.fail_json(changed=True, msg="Not enough parameters found.") 

test_project.py :

@patch('library.project.run_module') 
@patch('library.project.AnsibleModule') 
def test_main_exit_functionality_failure(mock_module, mock_run_module): 
    """ 
    project - test_main_exit_functionality - failure 
    """ 
    instance = mock_module.return_value 

    # What happens when the run_module returns false 
    # that is run_module fails 
    mock_run_module.return_value = False 

    project.main() 

    # AnsibleModule.exit_json should not activated 
    assert_equals(instance.fail_json.call_count, 0) 

    #AnsibleModule.fail_json should be called 
    instance.fail_json.assert_called_with(msg='Not enough parameters 
    specified.') 
+0

이 코드는 실제로 [최소한의 완전하고 검증 가능한 예제] (https://stackoverflow.com/help/mcve)는 아니며 문맥이없는 코드를 읽는 것은 매우 어렵습니다. 테스트 코드를주의 깊게 읽으면서 그것이 무엇을 말하는지 확실히 말하게해야합니다. 주된 문제는 마지막 줄이 될 것이라고 생각합니다. 예상되는 인수를'assert_called_with'에 _all_ 전달해야합니다. 'instance.fail_json.assert_called_with (changed = True, msg = '매개 변수가 충분하지 않습니다. ')' – ryanh119

+0

위의 표준을 반영하도록 코드를 다시 작성하겠습니다. 그러나 mock_run_module의 반환 값을 'False'로 설정 한 경우 : '''mock_run_module.return_value = False''' main의 if-else 분기가 false를 실행하여 module.fail_json changed = True, msg = "매개 변수가 충분하지 않습니다.")? 그게 왜 작동하지 않는지 내 주요 연결 끊기입니다. module.exit_json (changed = True, msg = ""프로젝트가 업데이트되었습니다. " – 1up

답변

0

생산 코드를 다시 읽어보십시오. 그것이 사실이라면 확인하기 전에 라인에 True로 return_val 설정 :

... 

return_val = run_module(module) 

return_val = True 

if return_val is True: 
    ... 

return_val 상관없이 그렇게 상관없이 테스트에 무엇을, 생산 코드는 항상 '진실을 실행하지 않습니다 무엇 run_module 반환, 항상 참입니다 'if-else 검사의 지점.