aboutsummaryrefslogtreecommitdiffstats
path: root/tests/helpers.py
blob: 2908294f765d3acbc13498b7f686d0aff0aebd04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import asyncio
import functools
from unittest.mock import MagicMock


__all__ = ('AsyncMock', 'async_test')


# TODO: Remove me on 3.8
class AsyncMock(MagicMock):
    async def __call__(self, *args, **kwargs):
        return super(AsyncMock, self).__call__(*args, **kwargs)


def async_test(wrapped):
    """
    Run a test case via asyncio.

    Example:

        >>> @async_test
        ... async def lemon_wins():
        ...     assert True
    """

    @functools.wraps(wrapped)
    def wrapper(*args, **kwargs):
        return asyncio.run(wrapped(*args, **kwargs))
    return wrapper