[Python] Python: Unpack call_args_list from unittest.mock

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    i would like to unpack the arguments of a mocked method. I have a mocked subscriber which is called by the code under test and i would like to verify the invocation of the notify() method.

    class Subscriber:
    def notify(self, event):
    pass


    I'm using the following snippet to unpack the arguments and verify two invocations:

    calls= self.subscriber.notify.call_args_list
    event1 = calls[0][0][0]
    event2 = calls[1][0][0]

    assert_that(event1, instance_of(CreatedEvent))
    assert_that(event1.file.name, equal_to("foo.txt"))


    But the two lines to unpack the events are very clumsy and far away from readable code. Does somebody know a better approach to unpack the arguments?

    Thanks a lot!

    Continue reading...

Compartilhe esta Página