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] Trying to understand mocking in python

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    using pytest - given the following:

    class Bar:
    def wtf(self):
    return 1
    class Foo:
    def __init__(self, bar):
    self.bar = bar
    def huh(self):
    return self.bar.wtf()


    def test_huh():
    bar = Bar()
    foo = Foo(bar)
    assert foo.huh() == 1

    def test_huh2(mocker):
    bar = mocker.Mock()
    foo = Foo(bar)
    bar.wtf().return_value = 1
    assert foo.huh() == 1


    test_huh does what I expect but I want to test Foo and mock the dependency on Bar.

    test_huh2 fails.

    it seems the mock does not get called. Can someone explain how this should be done?

    Continue reading...

Compartilhe esta Página