2017-09-17 95 views
-1

我想对我正在构建的一些代码实现一些单元测试,但我看到这种奇怪的行为,即使我将函数调用的返回值设置为False,相关代码不会执行,因此断言instance.fail_json.assert_called_with(msg='Not enough parameters specified.')失败。声明不在Python中执行模拟

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'。它应该读取'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 =“没有足够的参数指定。”)?为什么没有运行是我的主要脱节。 '''如果return_value为True: module.exit_json(changed = True,msg =“”Project updated。“) else: module.fail_json(changed = True,msg =”找不到足够的参数。 '' – 1up

回答

0

重读你的产品代码。它集RETURN_VAL为真就行了检查,如果它是真实的之前:

... 

return_val = run_module(module) 

return_val = True 

if return_val is True: 
    ... 

return_val始终是真实的,不管是什么run_module回报,所以无论你在测试中做什么,生产代码将始终执行“真'if-else支票的分支。