简体   繁体   中英

How to make two equal unittest.mock.Mock objects to be of the same type?

I have to ensure that a Sequence is homogeneous, ie contains only objects of similar type. So I added an assert like assert all(isinstance(obj, type(my_list[0])) for obj in my_list[1:]) That's not perfect but sufficient for my needs. Unfortunately this causes problems when using it with unittest.mock.Mock :

import unittest.mock as utm

class C:
    pass

my_list = [utm.Mock(spec_set=C), utm.Mock(spec_set=C)]

all(isinstance(obj, type(my_list[0])) for obj in my_list[1:])
>>>
False

In particular two mocks have different types, even if the have the same spec_set :

isinstance(utm.Mock(spec_set=C), type(utm.Mock(spec_set=C)))
>>>
False

Is there any way to configure the mocks so they are considered to be of the same type? Modifying the check would be an option too, as long as it does not get mock specific.

If that is of interest: I used Python 3.5.

why not do this?

class A:
    pass

class MockA(Mock, A):
    pass

a1 = MockA()
a2 = MockA()

assert isinstance(a1, MockA) == isinstance(a2, MockA)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM