customer compare in assertEqual

Peter Otten __peter__ at web.de
Tue Oct 30 03:52:40 EDT 2018


Stone Zhong wrote:

> Hi There,
> 
> Now I want to make sure my code calls a function foo with an object t,
> however, foo.assert_called_once_with(t) does not work, since t is a model
> object and the code may load a different copy of t, so what I really want
> to test is "It calls foo with t where t.id equals real_t.id, is there a
> way to do that in python unit test?

If you don't find any mock-specific solution you can provide a t with a 
custom equality operator:

class U:
    def __init__(self, id):
        self.id = id
    def __eq__(self, other):
        return self.id == other.id

foo.assert_called_once_with(U(42))

This should ensure that real_t.id == 42.




More information about the Python-list mailing list