[Python] python unittest How to patch a function with an injected argument and modify it

Discussão em 'Python' iniciado por Stack, Dezembro 9, 2024.

  1. Stack

    Stack Membro Participativo

    Try to unittest the function to test which used a function with an injected Element I know how to patch the return value of function_to_patch but how to alter the injected element ?

    @dataclass
    class Element:
    type: str
    name:str
    tags: List[str]
    nb_elt:int


    def function_to_patch(injected_arg:Element )-> Tuple[List[str], int]:
    do_something()

    injected_arg.tags=["tag1","tag3"]
    return (["somedata"], 0)


    def function_to_test()-> str:
    injected_arg= Element()
    do_something...
    mylist, myval = function_to_patch(injected_arg)
    ....
    return Element.tags[1]




    With this approach the result is not correct The result does not contain the expected "tag3" as I have no idea on how to alter the injected_arg

    @mock.patch("function_to_patch")
    def TestsFunctionToTest(self, patch_of_function_to_patch):

    patch_of_function_to_patch.return_value=("my return",3)

    result = function_to_test()
    assertEqual(result, "tag3")

    Continue reading...

Compartilhe esta Página