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] Mocking a class method attribute returns AttributeError

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    How can I mock the value of x to "bar" instead of "foo"?

    import pytest


    class Command():

    def run(self):
    x = "foo"
    return x


    def test(mocker):
    mocker.patch("myapp.tests.test_mocker.Command.run.x", "bar")
    c = Command()
    print(c.run())


    pytest returns AttributeError: <function Command.run at 0x7f4910e23f70> does not have the attribute 'x'

    https://pypi.org/project/pytest-mock/

    Continue reading...

Compartilhe esta Página