Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is spec and spec_set

Tags:

I am using Mock 1.0.1 python. In the path function definition there are two optional arguments names spec and spec_set (also auto_spec)

patch(target, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs) 

I have read through the documentation, but find no explanation of them. Maybe they are terms of testing? It will be nice if someone can give information, thank you.

like image 884
Hello lad Avatar asked Aug 15 '14 08:08

Hello lad


People also ask

What is the difference between mock and MagicMock?

Mock vs. So what is the difference between them? MagicMock is a subclass of Mock . It contains all magic methods pre-created and ready to use (e.g. __str__ , __len__ , etc.). Therefore, you should use MagicMock when you need magic methods, and Mock if you don't need them.

What does mock patch do?

mock provides a powerful mechanism for mocking objects, called patch() , which looks up an object in a given module and replaces that object with a Mock . Usually, you use patch() as a decorator or a context manager to provide a scope in which you will mock the target object.

What is Side_effect in mock python?

side_effect: A function to be called whenever the Mock is called. See the side_effect attribute. Useful for raising exceptions or dynamically changing return values. The function is called with the same arguments as the mock, and unless it returns DEFAULT , the return value of this function is used as the return value.

What is MagicMock Python?

MagicMock. MagicMock objects provide a simple mocking interface that allows you to set the return value or other behavior of the function or object creation call that you patched. This allows you to fully define the behavior of the call and avoid creating real objects, which can be onerous.


1 Answers

unittest.mock in Python 3.x is basically same with mock.

According to the unittest.mock documentation:

spec: This can be either a list of strings or an existing object (a class or instance) that acts as the specification for the mock object. If you pass in an object then a list of strings is formed by calling dir on the object (excluding unsupported magic attributes and methods). Accessing any attribute not in this list will raise an AttributeError.

If spec is an object (rather than a list of strings) then _class_ returns the class of the spec object. This allows mocks to pass isinstance tests.

spec_set: A stricter variant of spec. If used, attempting to set or get an attribute on the mock that isn’t on the object passed as spec_set will raise an AttributeError.


Update Difference between spec and spec_set.

With spec, you can set attribute that is not specified, while with spec_set, it is not allowed to set unspecified attribute.

Example:

>>> from unittest.mock import Mock >>> class A: ...     def __init__(self, a, b): ...         self.a = a ...         self.b = b ... >>> aobj = A(1, 2)    >>> m = Mock(spec=aobj)   # spec >>> m.c   # get -> fail Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/local/Cellar/python3/3.6.0b4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 582, in __getattr__     raise AttributeError("Mock object has no attribute %r" % name) AttributeError: Mock object has no attribute 'c' >>> m.c = 9  # set -> success >>> m.c      # get -> success (although c is not in the spec) 9    >>> m = Mock(spec_set=aobj)   # spec_set >>> m.a <Mock name='mock.a' id='4544967400'> >>> m.b <Mock name='mock.b' id='4545493928'> >>> m.c   # get -> fail Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/local/Cellar/python3/3.6.0b4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 582, in __getattr__     raise AttributeError("Mock object has no attribute %r" % name) AttributeError: Mock object has no attribute 'c' >>> m.c = 9  # set -> fail Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/local/Cellar/python3/3.6.0b4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 688, in __setattr__     raise AttributeError("Mock object has no attribute '%s'" % name) AttributeError: Mock object has no attribute 'c' 
like image 168
falsetru Avatar answered Oct 26 '22 20:10

falsetru