2021年9月17日 星期五

[ Python 常見問題 ] When using unittest.mock.patch, why is autospec not True by default?

 Source From Here

Question
When you patch a function using mock, you have the option to specify autospec as True:
If you set autospec=True then the mock with be created with a spec from the object being replaced. All attributes of the mock will also have the spec of the corresponding attribute of the object being replaced. Methods and functions being mocked will have their arguments checked and will raise a TypeError if they are called with the wrong signature.

(http://www.voidspace.org.uk/python/mock/patch.html)

I'm wondering why this isn't the default behavior? Surely we would almost always want to catch passing incorrect parameters to any function we patch?

HowTo
The only clear way to explain this, is to actually quote the documentation on the downside of using auto-speccing and why you should be careful when using it:
This isn’t without caveats and limitations however, which is why it is not the default behaviour. In order to know what attributes are available on the spec object, autospec has to introspect (access attributes) the spec. As you traverse attributes on the mock a corresponding traversal of the original object is happening under the hood. If any of your specced objects have properties or descriptors that can trigger code execution then you may not be able to use autospec. On the other hand it is much better to design your objects so that introspection is safe [4].

A more serious problem is that it is common for instance attributes to be created in the init method and not to exist on the class at all. autospec can’t know about any dynamically created attributes and restricts the api to visible attributes.

I think the key takeaway here is to note this line: autospec can’t know about any dynamically created attributes and restricts the api to visible attributes

So, to help being more explicit with an example of where autospeccing breaks, this example taken from the documentation shows this:
  1. >>> class Something:  
  2. ...   def __init__(self):  
  3. ...     self.a = 33  
  4. ...  
  5. >>> with patch('__main__.Something', autospec=True):  
  6. ...   thing = Something()  
  7. ...   thing.a  
  8. ...  
  9. Traceback (most recent call last):  
  10.   ...  
  11. AttributeError: Mock object has no attribute 'a'  
As you can see, auto-speccing has no idea that there is an attribute a being created when creating your Something object. There is nothing wrong with assigning a value to your instance attribute.

Observe the below functional example:
  1. import unittest  
  2. from mock import patch  
  3.   
  4. def some_external_thing():  
  5.     pass  
  6.   
  7. def something(x):  
  8.     return x  
  9.   
  10. class MyRealClass:  
  11.     def __init__(self):  
  12.         self.a = some_external_thing()  
  13.   
  14.     def test_thing(self):  
  15.         return something(self.a)  
  16.   
  17.   
  18.   
  19. class MyTest(unittest.TestCase):  
  20.     def setUp(self):  
  21.         self.my_obj = MyRealClass()  
  22.   
  23.     @patch('__main__.some_external_thing')      
  24.     @patch('__main__.something')  
  25.     def test_my_things(self, mock_something, mock_some_external_thing):  
  26.         mock_some_external_thing.return_value = "there be dragons"  
  27.         self.my_obj.a = mock_some_external_thing.return_value  
  28.         self.my_obj.test_thing()  
  29.   
  30.         mock_something.assert_called_once_with("there be dragons")  
  31.   
  32.   
  33. if __name__ == '__main__':  
  34.     unittest.main()  
So, I'm just saying for my test case I want to make sure that the some_external_thing() method does not affect the behaviour of my unittest, so I'm just assigning my instance attribute the mock per mock_some_external_thing.return_value = "there be dragons".

沒有留言:

張貼留言

[ Python 常見問題 ] When using unittest.mock.patch, why is autospec not True by default?

  Source From  Here Question When you patch a function using  mock , you have the option to specify  autospec  as True: If you set  autospec...