declarative-mocks 0.4.1__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {declarative_mocks-0.4.1 → declarative_mocks-0.5.0}/PKG-INFO +86 -27
- {declarative_mocks-0.4.1 → declarative_mocks-0.5.0}/README.md +83 -25
- {declarative_mocks-0.4.1 → declarative_mocks-0.5.0}/pyproject.toml +3 -2
- declarative_mocks-0.5.0/src/dmock/__init__.py +26 -0
- declarative_mocks-0.5.0/src/dmock/_interaction.py +1784 -0
- {declarative_mocks-0.4.1 → declarative_mocks-0.5.0}/src/dmock/_mock.py +88 -29
- {declarative_mocks-0.4.1 → declarative_mocks-0.5.0}/src/dmock/_protocols.py +5 -3
- {declarative_mocks-0.4.1 → declarative_mocks-0.5.0}/src/dmock/_pytest_plugin.py +7 -6
- {declarative_mocks-0.4.1 → declarative_mocks-0.5.0}/src/dmock/_registry.py +55 -2
- declarative_mocks-0.5.0/src/dmock/_spy.py +513 -0
- {declarative_mocks-0.4.1 → declarative_mocks-0.5.0}/src/dmock/_types.py +34 -2
- declarative_mocks-0.5.0/src/dmock/checks/__init__.py +13 -0
- declarative_mocks-0.5.0/src/dmock/checks/_check.py +174 -0
- declarative_mocks-0.5.0/src/dmock/exceptions/__init__.py +20 -0
- {declarative_mocks-0.4.1/src/dmock → declarative_mocks-0.5.0/src/dmock/exceptions}/_exceptions.py +41 -18
- {declarative_mocks-0.4.1/src/dmock → declarative_mocks-0.5.0/src/dmock/matchers}/__init__.py +6 -26
- {declarative_mocks-0.4.1/src/dmock → declarative_mocks-0.5.0/src/dmock/matchers}/_matchers.py +265 -76
- declarative_mocks-0.4.1/src/dmock/_expectation.py +0 -553
- {declarative_mocks-0.4.1 → declarative_mocks-0.5.0}/LICENSE +0 -0
- {declarative_mocks-0.4.1 → declarative_mocks-0.5.0}/src/dmock/py.typed +0 -0
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: declarative-mocks
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: Declare mock behavior as ordered expectations. Simple, typed, gomock-inspired DSL over unittest.mock
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
7
|
-
Keywords: mock,mocking,testing,unittest,pytest
|
|
7
|
+
Keywords: mock,mocking,spying,testing,unittest,pytest
|
|
8
8
|
Author: Dimitry Churilov
|
|
9
9
|
Author-email: d.mdx3r@gmail.com
|
|
10
10
|
Requires-Python: >=3.11
|
|
@@ -19,6 +19,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.12
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.13
|
|
21
21
|
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
22
23
|
Classifier: Topic :: Software Development :: Testing
|
|
23
24
|
Classifier: Topic :: Software Development :: Testing :: Mocking
|
|
24
25
|
Classifier: Typing :: Typed
|
|
@@ -29,7 +30,7 @@ Description-Content-Type: text/markdown
|
|
|
29
30
|
|
|
30
31
|

|
|
31
32
|

|
|
32
|
-

|
|
33
|
+

|
|
33
34
|

|
|
34
35
|
|
|
35
36
|
# declarative-mocks
|
|
@@ -69,17 +70,20 @@ service = Mock(spec=Directory)
|
|
|
69
70
|
service.lookup.return_value = User(id=1, name="Ada", email="ada@example.com")
|
|
70
71
|
|
|
71
72
|
user = service.lookup("ada")
|
|
72
|
-
assert
|
|
73
|
-
assert user.name
|
|
74
|
-
assert "@"
|
|
73
|
+
assert user.id == 1
|
|
74
|
+
assert user.name == "Ada"
|
|
75
|
+
assert user.email == "ada@example.com"
|
|
75
76
|
assert abs((datetime.now() - user.created_at).total_seconds()) < 5
|
|
76
|
-
service.lookup.
|
|
77
|
+
name = service.lookup.call_args.args[0]
|
|
78
|
+
assert isinstance(name, str)
|
|
79
|
+
assert name.startswith("a")
|
|
77
80
|
```
|
|
78
81
|
|
|
79
82
|
Here's an example of how **dmock** improves your tests:
|
|
80
83
|
|
|
81
84
|
```python
|
|
82
|
-
from dmock import
|
|
85
|
+
from dmock import DeclarativeMock
|
|
86
|
+
from dmock.matchers import IsNow, StartsWith
|
|
83
87
|
|
|
84
88
|
service = DeclarativeMock(Directory)
|
|
85
89
|
service.expect(Directory.lookup, StartsWith("a")).returns(
|
|
@@ -111,6 +115,9 @@ api.expect(ApiClient.next_page).returns({"items": [1], "cursor": "a"})
|
|
|
111
115
|
# or you could use the string form to produce a lenient expectation: any arguments, any outcome
|
|
112
116
|
api.expect("next_page").returns({"items": [2], "cursor": None})
|
|
113
117
|
# first call → first dict; second call → second dict
|
|
118
|
+
# Same args → registration order; different args → matched by args.
|
|
119
|
+
# Chain .expect(...) or use in_order(...) to enforce strict order across different calls:
|
|
120
|
+
api.expect("next_page", limit=5).returns({"items": []}).once().expect("close").raises(TimeoutError)
|
|
114
121
|
|
|
115
122
|
calc = DeclarativeMock(Calculator)
|
|
116
123
|
calc.expect("price", 100).runs(lambda x: x - 1).returns(99)
|
|
@@ -121,7 +128,8 @@ assert calc.price(100) == 99 # from .returns(99)
|
|
|
121
128
|
`expect(...)` with no further arguments matches a call with no arguments. A keyword matches only that keyword:
|
|
122
129
|
|
|
123
130
|
```python
|
|
124
|
-
from dmock import
|
|
131
|
+
from dmock import DeclarativeMock
|
|
132
|
+
from dmock.matchers import Anything
|
|
125
133
|
|
|
126
134
|
m = DeclarativeMock(Worker)
|
|
127
135
|
m.expect("run") # called with no arguments
|
|
@@ -132,7 +140,8 @@ m.expect("run", job_id=123).returns("queued") # this is keyword only
|
|
|
132
140
|
`ANY_ARGS` and `ANY_KWARGS` match any positional and keyword arguments, including none:
|
|
133
141
|
|
|
134
142
|
```python
|
|
135
|
-
from dmock import
|
|
143
|
+
from dmock import DeclarativeMock
|
|
144
|
+
from dmock.matchers import ANY_ARGS, ANY_KWARGS
|
|
136
145
|
|
|
137
146
|
m = DeclarativeMock(Worker)
|
|
138
147
|
m.expect("run", ANY_ARGS, ANY_KWARGS).returns(0) # any positional and keyword args, including none
|
|
@@ -147,7 +156,7 @@ calc = DeclarativeMock(Calculator)
|
|
|
147
156
|
calc.expect("price", 100).returns(99).once()
|
|
148
157
|
assert calc.price(100) == 99
|
|
149
158
|
|
|
150
|
-
calc.expect("price", 200).returns(99).times(3) # last outcome repeats for the extra calls
|
|
159
|
+
calc.expect("price", 200).returns(99, repeat=2).times(3) # last outcome repeats for the extra calls
|
|
151
160
|
assert [calc.price(200) for _ in range(3)] == [99, 99, 99]
|
|
152
161
|
|
|
153
162
|
calc.expect("price", 300).returns(99).between(1, 3)
|
|
@@ -160,13 +169,16 @@ calc.verify()
|
|
|
160
169
|
Async methods use the same DSL. `runs()` accepts a sync or an async function:
|
|
161
170
|
|
|
162
171
|
```python
|
|
163
|
-
from dmock import
|
|
172
|
+
from dmock import DeclarativeMock
|
|
173
|
+
from dmock.matchers import Anything
|
|
164
174
|
|
|
165
175
|
mock = DeclarativeMock(MyService)
|
|
166
176
|
mock.expect(MyService.aprocess_order, Anything()).returns("ok")
|
|
167
177
|
assert await mock.aprocess_order(1) == "ok"
|
|
168
178
|
|
|
169
|
-
mock.expect(MyService.aprocess_order, Anything()).runs(
|
|
179
|
+
mock.expect(MyService.aprocess_order, Anything()).runs(
|
|
180
|
+
lambda order_id: f"sync-{order_id}"
|
|
181
|
+
)
|
|
170
182
|
assert await mock.aprocess_order(7) == "sync-7"
|
|
171
183
|
|
|
172
184
|
async def async_fn(order_id: int) -> str:
|
|
@@ -179,9 +191,10 @@ assert await mock.aprocess_order(9) == "async-9"
|
|
|
179
191
|
The matcher set is rich enough that even the most complicated call stays as **one declarative expectation**. Prefixes, partial mappings, lengths, order, and formats compose in place:
|
|
180
192
|
|
|
181
193
|
```python
|
|
182
|
-
from
|
|
194
|
+
from typing import Any
|
|
195
|
+
from dmock import DeclarativeMock
|
|
196
|
+
from dmock.matchers import (
|
|
183
197
|
AnyOrder,
|
|
184
|
-
DeclarativeMock,
|
|
185
198
|
Equals,
|
|
186
199
|
GreaterThan,
|
|
187
200
|
HasLen,
|
|
@@ -191,7 +204,9 @@ from dmock import (
|
|
|
191
204
|
)
|
|
192
205
|
|
|
193
206
|
class EventBus:
|
|
194
|
-
def publish(
|
|
207
|
+
def publish(
|
|
208
|
+
self, *, topic: str, headers: dict[str, Any], payload: dict[str, Any]
|
|
209
|
+
) -> bool: ...
|
|
195
210
|
|
|
196
211
|
bus = DeclarativeMock(EventBus)
|
|
197
212
|
bus.expect(
|
|
@@ -206,12 +221,14 @@ bus.expect(
|
|
|
206
221
|
payload=PartialMapping(
|
|
207
222
|
{
|
|
208
223
|
"order_id": GreaterThan(0),
|
|
209
|
-
"items":
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
224
|
+
"items": (
|
|
225
|
+
HasLen(GreaterThan(0))
|
|
226
|
+
& AnyOrder(
|
|
227
|
+
[
|
|
228
|
+
PartialMapping({"sku": "SKU-AAA", "qty": 1}),
|
|
229
|
+
PartialMapping({"sku": "SKU-BBB", "qty": 2}),
|
|
230
|
+
]
|
|
231
|
+
)
|
|
215
232
|
),
|
|
216
233
|
}
|
|
217
234
|
),
|
|
@@ -237,16 +254,58 @@ assert bus.publish(
|
|
|
237
254
|
bus.verify()
|
|
238
255
|
```
|
|
239
256
|
|
|
240
|
-
**dmock**
|
|
257
|
+
**dmock** also supports pass-through spies using the same declarative DSL:
|
|
258
|
+
|
|
259
|
+
```python
|
|
260
|
+
from dmock import DeclarativeSpy
|
|
261
|
+
from dmock.checks import Check
|
|
262
|
+
|
|
263
|
+
class Store:
|
|
264
|
+
def __init__(self) -> None:
|
|
265
|
+
self.items: dict[str, str] = {}
|
|
266
|
+
self.dirty = False
|
|
267
|
+
|
|
268
|
+
def save(self, key: str, value: str) -> bool:
|
|
269
|
+
self.items[key] = value
|
|
270
|
+
self.dirty = False
|
|
271
|
+
return True
|
|
272
|
+
|
|
273
|
+
def fetch(self, key: str) -> str:
|
|
274
|
+
return self.items[key]
|
|
275
|
+
|
|
276
|
+
store = Store()
|
|
277
|
+
spy = DeclarativeSpy(store)
|
|
278
|
+
|
|
279
|
+
(
|
|
280
|
+
spy.observe(Store.save, "a", "A")
|
|
281
|
+
.returns(
|
|
282
|
+
True,
|
|
283
|
+
condition=Check[bool](
|
|
284
|
+
lambda _: store.items.get("a") == "A",
|
|
285
|
+
lambda _: not store.dirty,
|
|
286
|
+
),
|
|
287
|
+
)
|
|
288
|
+
.once()
|
|
289
|
+
.observe(Store.fetch, "a")
|
|
290
|
+
.returns("A")
|
|
291
|
+
.once()
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
assert spy.save("a", "A") is True
|
|
295
|
+
assert spy.fetch("a") == "A"
|
|
296
|
+
spy.verify()
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
**dmock** is built on `unittest.mock`, has zero runtime dependencies, and doesn't patch anything. Unregistered mock calls fail immediately. Nothing on live modules or instances is rewritten.
|
|
241
300
|
|
|
242
301
|
Look up the dmock source code and tests yourself, it's full of examples!
|
|
243
302
|
|
|
244
303
|
## Limitations & non-goals
|
|
245
304
|
|
|
246
|
-
- **No signature binding at runtime.** `expect("method", …)`
|
|
247
|
-
- **No automatic** `verify()` **outside pytest.** The pytest plugin checks every mock when the test finishes. Other runners still need an explicit `verify()`.
|
|
248
|
-
- **No
|
|
249
|
-
- **Not thread-safe.** Concurrent use of one mock from multiple threads is out of scope.
|
|
305
|
+
- **No signature binding at runtime.** `expect("method", …)` and `observe("method", …)` check that `method` exists on the spec or object, not that the argument list matches the real signature at runtime.
|
|
306
|
+
- **No automatic** `verify()` **outside pytest.** The pytest plugin checks every mock and spy when the test finishes. Other runners still need an explicit `verify()`.
|
|
307
|
+
- **No global patching.** The library does not patch modules or instances. Construct a `DeclarativeMock` or wrap a live instance with `DeclarativeSpy` and pass it in.
|
|
308
|
+
- **Not thread-safe.** Concurrent use of one mock or spy from multiple threads is out of scope.
|
|
250
309
|
|
|
251
310
|
## Roadmap
|
|
252
311
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|

|
|
2
2
|

|
|
3
|
-

|
|
3
|
+

|
|
4
4
|

|
|
5
5
|
|
|
6
6
|
# declarative-mocks
|
|
@@ -40,17 +40,20 @@ service = Mock(spec=Directory)
|
|
|
40
40
|
service.lookup.return_value = User(id=1, name="Ada", email="ada@example.com")
|
|
41
41
|
|
|
42
42
|
user = service.lookup("ada")
|
|
43
|
-
assert
|
|
44
|
-
assert user.name
|
|
45
|
-
assert "@"
|
|
43
|
+
assert user.id == 1
|
|
44
|
+
assert user.name == "Ada"
|
|
45
|
+
assert user.email == "ada@example.com"
|
|
46
46
|
assert abs((datetime.now() - user.created_at).total_seconds()) < 5
|
|
47
|
-
service.lookup.
|
|
47
|
+
name = service.lookup.call_args.args[0]
|
|
48
|
+
assert isinstance(name, str)
|
|
49
|
+
assert name.startswith("a")
|
|
48
50
|
```
|
|
49
51
|
|
|
50
52
|
Here's an example of how **dmock** improves your tests:
|
|
51
53
|
|
|
52
54
|
```python
|
|
53
|
-
from dmock import
|
|
55
|
+
from dmock import DeclarativeMock
|
|
56
|
+
from dmock.matchers import IsNow, StartsWith
|
|
54
57
|
|
|
55
58
|
service = DeclarativeMock(Directory)
|
|
56
59
|
service.expect(Directory.lookup, StartsWith("a")).returns(
|
|
@@ -82,6 +85,9 @@ api.expect(ApiClient.next_page).returns({"items": [1], "cursor": "a"})
|
|
|
82
85
|
# or you could use the string form to produce a lenient expectation: any arguments, any outcome
|
|
83
86
|
api.expect("next_page").returns({"items": [2], "cursor": None})
|
|
84
87
|
# first call → first dict; second call → second dict
|
|
88
|
+
# Same args → registration order; different args → matched by args.
|
|
89
|
+
# Chain .expect(...) or use in_order(...) to enforce strict order across different calls:
|
|
90
|
+
api.expect("next_page", limit=5).returns({"items": []}).once().expect("close").raises(TimeoutError)
|
|
85
91
|
|
|
86
92
|
calc = DeclarativeMock(Calculator)
|
|
87
93
|
calc.expect("price", 100).runs(lambda x: x - 1).returns(99)
|
|
@@ -92,7 +98,8 @@ assert calc.price(100) == 99 # from .returns(99)
|
|
|
92
98
|
`expect(...)` with no further arguments matches a call with no arguments. A keyword matches only that keyword:
|
|
93
99
|
|
|
94
100
|
```python
|
|
95
|
-
from dmock import
|
|
101
|
+
from dmock import DeclarativeMock
|
|
102
|
+
from dmock.matchers import Anything
|
|
96
103
|
|
|
97
104
|
m = DeclarativeMock(Worker)
|
|
98
105
|
m.expect("run") # called with no arguments
|
|
@@ -103,7 +110,8 @@ m.expect("run", job_id=123).returns("queued") # this is keyword only
|
|
|
103
110
|
`ANY_ARGS` and `ANY_KWARGS` match any positional and keyword arguments, including none:
|
|
104
111
|
|
|
105
112
|
```python
|
|
106
|
-
from dmock import
|
|
113
|
+
from dmock import DeclarativeMock
|
|
114
|
+
from dmock.matchers import ANY_ARGS, ANY_KWARGS
|
|
107
115
|
|
|
108
116
|
m = DeclarativeMock(Worker)
|
|
109
117
|
m.expect("run", ANY_ARGS, ANY_KWARGS).returns(0) # any positional and keyword args, including none
|
|
@@ -118,7 +126,7 @@ calc = DeclarativeMock(Calculator)
|
|
|
118
126
|
calc.expect("price", 100).returns(99).once()
|
|
119
127
|
assert calc.price(100) == 99
|
|
120
128
|
|
|
121
|
-
calc.expect("price", 200).returns(99).times(3) # last outcome repeats for the extra calls
|
|
129
|
+
calc.expect("price", 200).returns(99, repeat=2).times(3) # last outcome repeats for the extra calls
|
|
122
130
|
assert [calc.price(200) for _ in range(3)] == [99, 99, 99]
|
|
123
131
|
|
|
124
132
|
calc.expect("price", 300).returns(99).between(1, 3)
|
|
@@ -131,13 +139,16 @@ calc.verify()
|
|
|
131
139
|
Async methods use the same DSL. `runs()` accepts a sync or an async function:
|
|
132
140
|
|
|
133
141
|
```python
|
|
134
|
-
from dmock import
|
|
142
|
+
from dmock import DeclarativeMock
|
|
143
|
+
from dmock.matchers import Anything
|
|
135
144
|
|
|
136
145
|
mock = DeclarativeMock(MyService)
|
|
137
146
|
mock.expect(MyService.aprocess_order, Anything()).returns("ok")
|
|
138
147
|
assert await mock.aprocess_order(1) == "ok"
|
|
139
148
|
|
|
140
|
-
mock.expect(MyService.aprocess_order, Anything()).runs(
|
|
149
|
+
mock.expect(MyService.aprocess_order, Anything()).runs(
|
|
150
|
+
lambda order_id: f"sync-{order_id}"
|
|
151
|
+
)
|
|
141
152
|
assert await mock.aprocess_order(7) == "sync-7"
|
|
142
153
|
|
|
143
154
|
async def async_fn(order_id: int) -> str:
|
|
@@ -150,9 +161,10 @@ assert await mock.aprocess_order(9) == "async-9"
|
|
|
150
161
|
The matcher set is rich enough that even the most complicated call stays as **one declarative expectation**. Prefixes, partial mappings, lengths, order, and formats compose in place:
|
|
151
162
|
|
|
152
163
|
```python
|
|
153
|
-
from
|
|
164
|
+
from typing import Any
|
|
165
|
+
from dmock import DeclarativeMock
|
|
166
|
+
from dmock.matchers import (
|
|
154
167
|
AnyOrder,
|
|
155
|
-
DeclarativeMock,
|
|
156
168
|
Equals,
|
|
157
169
|
GreaterThan,
|
|
158
170
|
HasLen,
|
|
@@ -162,7 +174,9 @@ from dmock import (
|
|
|
162
174
|
)
|
|
163
175
|
|
|
164
176
|
class EventBus:
|
|
165
|
-
def publish(
|
|
177
|
+
def publish(
|
|
178
|
+
self, *, topic: str, headers: dict[str, Any], payload: dict[str, Any]
|
|
179
|
+
) -> bool: ...
|
|
166
180
|
|
|
167
181
|
bus = DeclarativeMock(EventBus)
|
|
168
182
|
bus.expect(
|
|
@@ -177,12 +191,14 @@ bus.expect(
|
|
|
177
191
|
payload=PartialMapping(
|
|
178
192
|
{
|
|
179
193
|
"order_id": GreaterThan(0),
|
|
180
|
-
"items":
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
194
|
+
"items": (
|
|
195
|
+
HasLen(GreaterThan(0))
|
|
196
|
+
& AnyOrder(
|
|
197
|
+
[
|
|
198
|
+
PartialMapping({"sku": "SKU-AAA", "qty": 1}),
|
|
199
|
+
PartialMapping({"sku": "SKU-BBB", "qty": 2}),
|
|
200
|
+
]
|
|
201
|
+
)
|
|
186
202
|
),
|
|
187
203
|
}
|
|
188
204
|
),
|
|
@@ -208,16 +224,58 @@ assert bus.publish(
|
|
|
208
224
|
bus.verify()
|
|
209
225
|
```
|
|
210
226
|
|
|
211
|
-
**dmock**
|
|
227
|
+
**dmock** also supports pass-through spies using the same declarative DSL:
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
from dmock import DeclarativeSpy
|
|
231
|
+
from dmock.checks import Check
|
|
232
|
+
|
|
233
|
+
class Store:
|
|
234
|
+
def __init__(self) -> None:
|
|
235
|
+
self.items: dict[str, str] = {}
|
|
236
|
+
self.dirty = False
|
|
237
|
+
|
|
238
|
+
def save(self, key: str, value: str) -> bool:
|
|
239
|
+
self.items[key] = value
|
|
240
|
+
self.dirty = False
|
|
241
|
+
return True
|
|
242
|
+
|
|
243
|
+
def fetch(self, key: str) -> str:
|
|
244
|
+
return self.items[key]
|
|
245
|
+
|
|
246
|
+
store = Store()
|
|
247
|
+
spy = DeclarativeSpy(store)
|
|
248
|
+
|
|
249
|
+
(
|
|
250
|
+
spy.observe(Store.save, "a", "A")
|
|
251
|
+
.returns(
|
|
252
|
+
True,
|
|
253
|
+
condition=Check[bool](
|
|
254
|
+
lambda _: store.items.get("a") == "A",
|
|
255
|
+
lambda _: not store.dirty,
|
|
256
|
+
),
|
|
257
|
+
)
|
|
258
|
+
.once()
|
|
259
|
+
.observe(Store.fetch, "a")
|
|
260
|
+
.returns("A")
|
|
261
|
+
.once()
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
assert spy.save("a", "A") is True
|
|
265
|
+
assert spy.fetch("a") == "A"
|
|
266
|
+
spy.verify()
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
**dmock** is built on `unittest.mock`, has zero runtime dependencies, and doesn't patch anything. Unregistered mock calls fail immediately. Nothing on live modules or instances is rewritten.
|
|
212
270
|
|
|
213
271
|
Look up the dmock source code and tests yourself, it's full of examples!
|
|
214
272
|
|
|
215
273
|
## Limitations & non-goals
|
|
216
274
|
|
|
217
|
-
- **No signature binding at runtime.** `expect("method", …)`
|
|
218
|
-
- **No automatic** `verify()` **outside pytest.** The pytest plugin checks every mock when the test finishes. Other runners still need an explicit `verify()`.
|
|
219
|
-
- **No
|
|
220
|
-
- **Not thread-safe.** Concurrent use of one mock from multiple threads is out of scope.
|
|
275
|
+
- **No signature binding at runtime.** `expect("method", …)` and `observe("method", …)` check that `method` exists on the spec or object, not that the argument list matches the real signature at runtime.
|
|
276
|
+
- **No automatic** `verify()` **outside pytest.** The pytest plugin checks every mock and spy when the test finishes. Other runners still need an explicit `verify()`.
|
|
277
|
+
- **No global patching.** The library does not patch modules or instances. Construct a `DeclarativeMock` or wrap a live instance with `DeclarativeSpy` and pass it in.
|
|
278
|
+
- **Not thread-safe.** Concurrent use of one mock or spy from multiple threads is out of scope.
|
|
221
279
|
|
|
222
280
|
## Roadmap
|
|
223
281
|
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "declarative-mocks"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.5.0"
|
|
4
4
|
description = "Declare mock behavior as ordered expectations. Simple, typed, gomock-inspired DSL over unittest.mock"
|
|
5
5
|
authors = [{ name = "Dimitry Churilov", email = "d.mdx3r@gmail.com" }]
|
|
6
6
|
license = "MIT"
|
|
7
7
|
license-files = ["LICENSE"]
|
|
8
8
|
readme = "README.md"
|
|
9
9
|
requires-python = ">=3.11"
|
|
10
|
-
keywords = ["mock", "mocking", "testing", "unittest", "pytest"]
|
|
10
|
+
keywords = ["mock", "mocking", "spying", "testing", "unittest", "pytest"]
|
|
11
11
|
classifiers = [
|
|
12
12
|
"Framework :: Pytest",
|
|
13
13
|
"Intended Audience :: Developers",
|
|
@@ -20,6 +20,7 @@ classifiers = [
|
|
|
20
20
|
"Programming Language :: Python :: 3.12",
|
|
21
21
|
"Programming Language :: Python :: 3.13",
|
|
22
22
|
"Programming Language :: Python :: 3.14",
|
|
23
|
+
"Programming Language :: Python :: 3.15",
|
|
23
24
|
"Topic :: Software Development :: Testing",
|
|
24
25
|
"Topic :: Software Development :: Testing :: Mocking",
|
|
25
26
|
"Typing :: Typed",
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Declarative wrapper around unittest.mock with a DSL for tests."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
6
|
+
|
|
7
|
+
from dmock._interaction import Expectation, Observation, in_order
|
|
8
|
+
from dmock._mock import DeclarativeMock, dmock
|
|
9
|
+
from dmock._spy import DeclarativeSpy, dspy
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
__version__ = version("declarative-mocks")
|
|
14
|
+
except PackageNotFoundError: # pragma: no cover
|
|
15
|
+
__version__ = "0.0.0+unknown"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"DeclarativeMock",
|
|
20
|
+
"DeclarativeSpy",
|
|
21
|
+
"Expectation",
|
|
22
|
+
"Observation",
|
|
23
|
+
"dmock",
|
|
24
|
+
"dspy",
|
|
25
|
+
"in_order",
|
|
26
|
+
]
|