Skip to content

Commit f4a88dd

Browse files
committed
Add testing utils
1 parent 7df93ed commit f4a88dd

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tests/utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# from unittest import TestCase
2+
3+
4+
def call(*args, **kwargs):
5+
return (tuple(args), dict(kwargs))
6+
7+
8+
class Mock:
9+
"""A mock callable object that stores its calls."""
10+
11+
def __init__(self):
12+
self._calls = []
13+
14+
def __call__(self, *args, **kwargs):
15+
self._calls.append(call(*args, **kwargs))
16+
17+
def assert_called_with(self, *args, **kwargs):
18+
# First call should be self, so we prepend it
19+
expected_args = [self] + list(args)
20+
expectation = call(*expected_args, **kwargs)
21+
22+
# Try to have a useful output for assertion failures
23+
assert self._calls[-1] == expectation, "Expected call with {}, got {}".format(
24+
expectation, self._calls[-1]
25+
)
26+
27+
28+
class AsyncMock(Mock):
29+
"""An async version of Mock that can be awaited."""
30+
31+
async def __call__(self, *args, **kwargs):
32+
return super().__call__(self, *args, **kwargs)
33+
34+
def assert_awaited_with(self, *args, **kwargs):
35+
return super().assert_called_with(*args, **kwargs)
36+
37+
def assert_has_awaits(self, awaits):
38+
assert self._calls == awaits

0 commit comments

Comments
 (0)