Import upstream version 1.2.2
Kali Janitor
3 years ago
0 | 0 | [bumpversion] |
1 | current_version = 1.2.0 | |
1 | current_version = 1.2.2 | |
2 | 2 | commit = True |
3 | 3 | message = Bump version to {new_version} |
4 | 4 | tag = True |
0 | 0 | MOCKITO CHANGE LOG |
1 | 1 | ================== |
2 | ||
3 | Release 1.2.2 (September 9, 2020) | |
4 | --------------------------------- | |
5 | ||
6 | - Fix typo in ``spy2`` doc | |
7 | ||
8 | ||
9 | Release 1.2.1 (February 19, 2020) | |
10 | --------------------------------- | |
11 | ||
12 | - @nielsvaneck fixed how we can lookup inherited classmethods. | |
13 | ||
2 | 14 | |
3 | 15 | Release 1.2.0 (November 25, 2019) |
4 | 16 | --------------------------------- |
7 | 19 | - Fine tune error messages on unexpected calls or verifications. E.g. if you expect ``when(dog).bark('Wuff')`` but on call time do ``dog.bark('Wufff')``. Likewise, if you call ``dog.bark('Miau')`` and then ``verify(dog).bark('Maui')``. |
8 | 20 | - @felixonmars fixed a small compatibility issue with python 3.8 |
9 | 21 | - Mocking properties has become a bit easier. (#26) E.g. |
10 | ||
22 | ||
11 | 23 | prop = mock() |
12 | 24 | when(m).__get__(...).thenReturn(23) |
13 | 25 | m = mock({'name': prop}) |
14 | ||
26 | ||
15 | 27 | |
16 | 28 | Release 1.1.1 (August 28, 2018) |
17 | 29 | ------------------------------- |
28 | 40 | - Added ``forget_invocations`` function. Thanks to @maximkulkin |
29 | 41 | |
30 | 42 | This is generally useful if you already call mocks during your setup routine. |
31 | Now you could call ``forget_invocations`` at the end of your setup, and | |
43 | Now you could call ``forget_invocations`` at the end of your setup, and | |
32 | 44 | have a clean 'recording' for your actual test code. T.i. you don't have |
33 | 45 | to count the invocations from your setup code anymore. |
34 | 46 |
0 | bumpversion release --tag | |
1 | bumpversion patch --no-tag --message "master is {new_version}"⏎ | |
0 | bump2version release --tag | |
1 | bump2version patch --no-tag --message "master is {new_version}" |
12 | 12 | |
13 | 13 | So in difference to traditional patching, in mockito you always specify concrete arguments (a call signature), and its outcome, usually a return value via `thenReturn` or a raised exception via `thenRaise`. That effectively turns function calls into constants for the time of the test. |
14 | 14 | |
15 | Do **not** forget to :func:`unstub` of course! | |
15 | There are of course reasons when you don't want to overspecify specific tests. You _just_ want the desired stub answer. Here we go:: | |
16 | ||
17 | when(os.path).exists(...).thenReturn(True) | |
18 | ||
19 | # now, obviously, you get the same answer, regardless of the arguments | |
20 | os.path.exists('FooBar') # => True | |
21 | ||
22 | You can combine both stubs. E.g. nothing exists, except one file:: | |
23 | ||
24 | when(os.path).exists(...).thenReturn(False) | |
25 | when(os.path).exists('.flake8').thenReturn(True) | |
26 | ||
27 | And because it's a similar pattern, we can introduce :func:`spy2` here. Spies call through the original implementation of a given function. E.g. everything is as it is, except `'.flake8'` is just not there:: | |
28 | ||
29 | from mockito import spy2 | |
30 | spy2(os.path.exists) | |
31 | when(os.path).exists('.flake8').thenReturn(False) | |
32 | ||
33 | When patching, you **MUST** **not** forget to :func:`unstub` of course! You can do this explicitly | |
16 | 34 | |
17 | 35 | :: |
18 | 36 | |
19 | 37 | from mockito import unstub |
20 | 38 | unstub() # restore os.path module |
21 | 39 | |
22 | ||
23 | Now we mix global module patching with mocks. We want to test the following function using the fab `requests` library:: | |
40 | Usually you do this unconditionally in your `teardown` function. If you're using `pytest`, you could define a fixture instead | |
41 | ||
42 | :: | |
43 | # conftest.py | |
44 | import pytest | |
45 | ||
46 | @pytest.fixture | |
47 | def unstub(): | |
48 | from mockito import unstub | |
49 | yield | |
50 | unstub() | |
51 | ||
52 | # my_test.py | |
53 | import pytest | |
54 | pytestmark = pytest.mark.usefixtures("unstub") | |
55 | ||
56 | But very often you just use context managers (aka `with`), and mockito will unstub on 'exit' automatically:: | |
57 | ||
58 | # E.g. test that `exists` gets never called | |
59 | with expect(os.path, times=0).exists('.flake8'): | |
60 | # within the block `os.path.exists` is patched | |
61 | cached_dir_lookup('.flake8') | |
62 | # at the end of the block `os.path` gets unpatched | |
63 | ||
64 | # Which is btw roughly the same as doing | |
65 | with when(os.path).exists('.flake8'): | |
66 | cached_dir_lookup('.flake8') | |
67 | verify(os.path, times=0).exists(...) | |
68 | ||
69 | Now let's mix global module patching with mocks. We want to test the following function using the fab `requests` library:: | |
24 | 70 | |
25 | 71 | import requests |
26 | 72 |
43 | 43 | from .matchers import any, contains, times |
44 | 44 | from .verification import never |
45 | 45 | |
46 | __version__ = '1.2.0' | |
46 | __version__ = '1.2.2' | |
47 | 47 | |
48 | 48 | __all__ = [ |
49 | 49 | 'mock', |
17 | 17 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
18 | 18 | # THE SOFTWARE. |
19 | 19 | |
20 | import functools | |
21 | import inspect | |
22 | import operator | |
20 | 23 | from collections import deque |
21 | import inspect | |
22 | import functools | |
23 | import operator | |
24 | ||
25 | from . import invocation | |
26 | from . import signature | |
27 | from . import utils | |
24 | ||
25 | from . import invocation, signature, utils | |
28 | 26 | from .mock_registry import mock_registry |
29 | ||
30 | 27 | |
31 | 28 | __all__ = ['mock'] |
32 | 29 | |
74 | 71 | # STUBBING |
75 | 72 | |
76 | 73 | def get_original_method(self, method_name): |
74 | """ | |
75 | Looks up the original method on the `spec` object and returns it | |
76 | together with an indication of whether the method is found | |
77 | "directly" on the `spec` object. | |
78 | ||
79 | This is used to decide whether the method should be stored as an | |
80 | original_method and should therefore be replaced when unstubbing. | |
81 | """ | |
77 | 82 | if self.spec is None: |
78 | return None | |
83 | return None, False | |
79 | 84 | |
80 | 85 | try: |
81 | return self.spec.__dict__.get(method_name) | |
82 | except AttributeError: | |
83 | return getattr(self.spec, method_name, None) | |
86 | return self.spec.__dict__[method_name], True | |
87 | except (AttributeError, KeyError): | |
88 | # Classes with defined `__slots__` and then no `__dict__` are not | |
89 | # patchable but if we catch the `AttributeError` here, we get | |
90 | # the better error message for the user. | |
91 | return getattr(self.spec, method_name, None), False | |
84 | 92 | |
85 | 93 | def set_method(self, method_name, new_method): |
86 | 94 | setattr(self.mocked_obj, method_name, new_method) |
129 | 137 | try: |
130 | 138 | self.original_methods[method_name] |
131 | 139 | except KeyError: |
132 | original_method = self.get_original_method(method_name) | |
133 | self.original_methods[method_name] = original_method | |
140 | original_method, was_in_spec = self.get_original_method( | |
141 | method_name) | |
142 | if was_in_spec: | |
143 | # This indicates the original method was found directly on | |
144 | # the spec object and should therefore be restored by unstub | |
145 | self.original_methods[method_name] = original_method | |
146 | else: | |
147 | self.original_methods[method_name] = None | |
134 | 148 | |
135 | 149 | self.replace_method(method_name, original_method) |
136 | 150 | |
231 | 245 | `when`:: |
232 | 246 | |
233 | 247 | dummy = mock() |
234 | when(dummy).__call_(1).thenReturn(2) | |
248 | when(dummy).__call__(1).thenReturn(2) | |
235 | 249 | |
236 | 250 | All other magic methods must be configured this way or they will raise an |
237 | 251 | AttributeError. |
87 | 87 | E.g.:: |
88 | 88 | |
89 | 89 | import time |
90 | spy(time.time) | |
90 | spy2(time.time) | |
91 | 91 | do_work(...) # nothing injected, uses global patched `time` module |
92 | 92 | verify(time).time() |
93 | 93 |
13 | 13 | |
14 | 14 | |
15 | 15 | def newmethod(fn, obj): |
16 | if PY3: | |
17 | return types.MethodType(fn, obj) | |
18 | else: | |
19 | return types.MethodType(fn, obj, obj.__class__) | |
16 | return types.MethodType(fn, obj) | |
20 | 17 | |
21 | 18 | |
22 | 19 | def get_function_host(fn): |
17 | 17 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
18 | 18 | # THE SOFTWARE. |
19 | 19 | |
20 | from mockito import unstub, verify, when | |
21 | from mockito.verification import VerificationError | |
22 | ||
20 | 23 | from .test_base import TestBase |
21 | from mockito import when, unstub, verify | |
22 | from mockito.verification import VerificationError | |
24 | ||
23 | 25 | |
24 | 26 | class Dog: |
25 | 27 | @classmethod |
104 | 106 | self.assertEqual("Cat foo", Cat.meow("foo")) |
105 | 107 | |
106 | 108 | |
109 | class Retriever: | |
110 | @classmethod | |
111 | def retrieve(cls, item): | |
112 | return item | |
113 | ||
114 | ||
115 | class TrickDog(Dog, Retriever): | |
116 | pass | |
117 | ||
118 | ||
119 | class InheritedClassMethodsTest(TestBase): | |
120 | ||
121 | def tearDown(self): | |
122 | unstub() | |
123 | ||
124 | def testUnstubs(self): | |
125 | when(TrickDog).bark().thenReturn("miau!") | |
126 | when(TrickDog).retrieve("stick").thenReturn("ball") | |
127 | unstub() | |
128 | self.assertEqual("woof!", TrickDog.bark()) | |
129 | self.assertEqual("stick", TrickDog.retrieve("stick")) | |
130 | ||
131 | def testStubs(self): | |
132 | self.assertEqual("woof!", TrickDog.bark()) | |
133 | self.assertEqual("stick", TrickDog.retrieve("stick")) | |
134 | ||
135 | when(TrickDog).bark().thenReturn("miau!") | |
136 | when(TrickDog).retrieve("stick").thenReturn("ball") | |
137 | ||
138 | self.assertEqual("miau!", TrickDog.bark()) | |
139 | self.assertEqual("ball", TrickDog.retrieve("stick")) | |
140 | ||
141 | def testVerifiesMultipleCallsOnClassmethod(self): | |
142 | when(TrickDog).bark().thenReturn("miau!") | |
143 | when(TrickDog).retrieve("stick").thenReturn("ball") | |
144 | ||
145 | TrickDog.bark() | |
146 | TrickDog.bark() | |
147 | ||
148 | TrickDog.retrieve("stick") | |
149 | TrickDog.retrieve("stick") | |
150 | ||
151 | verify(TrickDog, times=2).bark() | |
152 | verify(TrickDog, times=2).retrieve("stick") | |
153 | ||
154 | def testFailsVerificationOfMultipleCallsOnClassmethod(self): | |
155 | when(TrickDog).bark().thenReturn("miau!") | |
156 | when(TrickDog).retrieve("stick").thenReturn("bark") | |
157 | ||
158 | TrickDog.bark() | |
159 | TrickDog.retrieve("stick") | |
160 | ||
161 | self.assertRaises(VerificationError, verify(TrickDog, times=2).bark) | |
162 | self.assertRaises(VerificationError, verify(TrickDog, | |
163 | times=2).retrieve) | |
164 | ||
165 | def testStubsAndVerifiesClassmethod(self): | |
166 | when(TrickDog).bark().thenReturn("miau!") | |
167 | when(TrickDog).retrieve("stick").thenReturn("ball") | |
168 | ||
169 | self.assertEqual("miau!", TrickDog.bark()) | |
170 | self.assertEqual("ball", TrickDog.retrieve("stick")) | |
171 | ||
172 | verify(TrickDog).bark() | |
173 | verify(TrickDog).retrieve("stick") | |
174 | ||
175 | def testPreservesSuperClassClassMethodWhenStubbed(self): | |
176 | self.assertEqual("woof!", Dog.bark()) | |
177 | self.assertEqual("stick", Retriever.retrieve("stick")) | |
178 | ||
179 | self.assertEqual("woof!", TrickDog.bark()) | |
180 | self.assertEqual("stick", TrickDog.retrieve("stick")) | |
181 | ||
182 | when(TrickDog).bark().thenReturn("miau!") | |
183 | when(TrickDog).retrieve("stick").thenReturn("ball") | |
184 | ||
185 | self.assertEqual("miau!", TrickDog.bark()) | |
186 | self.assertEqual("ball", TrickDog.retrieve("stick")) | |
187 | ||
188 | self.assertEqual("woof!", Dog.bark()) | |
189 | self.assertEqual("stick", Retriever.retrieve("stick")) | |
190 | ||
191 | def testDoubleStubStubWorksAfterUnstub(self): | |
192 | when(TrickDog).retrieve("stick").thenReturn("ball") | |
193 | when(TrickDog).retrieve("stick").thenReturn("cat") | |
194 | unstub() | |
195 | self.assertEqual("stick", TrickDog.retrieve("stick")) | |
196 | ||
197 | def testUnStubWorksOnClassAndSuperClass(self): | |
198 | self.assertEqual("stick", Retriever.retrieve("stick")) | |
199 | self.assertEqual("stick", TrickDog.retrieve("stick")) | |
200 | ||
201 | when(Retriever).retrieve("stick").thenReturn("ball") | |
202 | self.assertEqual("ball", Retriever.retrieve("stick")) | |
203 | self.assertEqual("ball", TrickDog.retrieve("stick")) | |
204 | ||
205 | when(TrickDog).retrieve("stick").thenReturn("cat") | |
206 | self.assertEqual("ball", Retriever.retrieve("stick")) | |
207 | self.assertEqual("cat", TrickDog.retrieve("stick")) | |
208 | ||
209 | unstub(TrickDog) | |
210 | self.assertEqual("ball", Retriever.retrieve("stick")) | |
211 | self.assertEqual("ball", TrickDog.retrieve("stick")) | |
212 | ||
213 | unstub(Retriever) | |
214 | self.assertEqual("stick", Retriever.retrieve("stick")) | |
215 | self.assertEqual("stick", TrickDog.retrieve("stick")) | |
216 | ||
217 | def testReverseOrderWhenUnstubbing(self): | |
218 | when(Retriever).retrieve("stick").thenReturn("ball") | |
219 | when(TrickDog).retrieve("stick").thenReturn("cat") | |
220 | ||
221 | unstub(Retriever) | |
222 | self.assertEqual("stick", Retriever.retrieve("stick")) | |
223 | self.assertEqual("cat", TrickDog.retrieve("stick")) | |
224 | ||
225 | unstub(TrickDog) | |
226 | self.assertEqual("stick", Retriever.retrieve("stick")) | |
227 | self.assertEqual("stick", TrickDog.retrieve("stick")) |