Can I fake/mock the type of my mock objects in python unittests

后端 未结 2 994
陌清茗
陌清茗 2020-12-15 04:21

In my python code I check the type of one of the parameters to make sure it is of the type I expect. For instance:

def myfunction(dbConnection):
    if (ty         


        
相关标签:
2条回答
  • 2020-12-15 04:43

    This is more-or-less exactly why you shouldn't enforce strict typechecking! You should remove that line from the code entirely.

    If you don't want to do that, write an abstract base class with the properties you want to have (.connect(), .cursor(), ...?) and check isinstance of that.

    Also <> has been obsolete for aaages. Use !=.

    0 讨论(0)
  • 2020-12-15 04:53

    With all due respect, It looks like you guys are not quite right!

    I can use duck typing as said, but there is a way to do what I intended to do in the first place:

    from http://docs.python.org/dev/library/unittest.mock.html

    Mock objects that use a class or an instance as a spec or spec_set are able to pass isintance tests:

    >>>
    >>> mock = Mock(spec=SomeClass)
    >>> isinstance(mock, SomeClass)
    True
    >>> mock = Mock(spec_set=SomeClass())
    >>> isinstance(mock, SomeClass)
    True
    

    so my example code would be like:

    m = mock.MagicMock(spec=bpgsql.Connection)
    isinstance(m, bpgsql.Connection) 
    

    this returns True

    All that said, I am not arguing for strict type checking in python, I say if you need to check it you can do it and it works with testing and mocking too.

    0 讨论(0)
提交回复
热议问题