1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python mock.patch: replace a method

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to replace a method in a class with mock:

    from unittest.mock import patch

    class A(object):
    def method(self, string):
    print(self, "method", string)

    def method2(self, string):
    print(self, "method2", string)

    with patch.object(A, 'method', side_effect=method2):
    a = A()
    a.method("string")
    a.method.assert_called_with("string")


    ...but I get insulted by the computer:

    TypeError: method2() missing 1 required positional argument: 'string'

    Continue reading...

Compartilhe esta Página