aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/aioresponses/tests/base.py
blob: 7db01b967f281fa58b307e333c420cf2c5556f6d (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
import asyncio
import sys

try:
    # as from Py3.8 unittest supports coroutines as test functions
    from unittest import IsolatedAsyncioTestCase, skipIf


    def fail_on(**kw):  # noqa
        def outer(fn):
            def inner(*args, **kwargs):
                return fn(*args, **kwargs)

            return inner

        return outer


except ImportError:
    # fallback to asynctest
    from asynctest import fail_on, skipIf
    from asynctest.case import TestCase as IsolatedAsyncioTestCase

IS_GTE_PY38 = sys.version_info >= (3, 8)


class AsyncTestCase(IsolatedAsyncioTestCase):
    """Asynchronous test case class that covers up differences in usage
    between unittest (starting from Python 3.8) and asynctest.

    `setup` and `teardown` is used to be called before each test case
    (note: that they are in lowercase)
    """

    async def setup(self):
        pass

    async def teardown(self):
        pass

    if IS_GTE_PY38:
        # from Python3.8
        async def asyncSetUp(self):
            self.loop = asyncio.get_event_loop()
            await self.setup()

        async def asyncTearDown(self):
            await self.teardown()
    else:
        # asynctest
        use_default_loop = False

        async def setUp(self) -> None:
            await self.setup()

        async def tearDown(self) -> None:
            await self.teardown()