import asyncio


def async_test(fn):
    """Decorator for making async methods usable as tests.

    This decorator also runs the async_setup method before each test,
    if it exists.

    """

    def wrapper(self, *args, **kwargs):
        async def sequence():
            if hasattr(self, "async_setup"):
                await self.async_setup()
            return await fn(self, *args, **kwargs)

        return asyncio.run(sequence(*args, **kwargs))  # pylint: disable=no-member

    return wrapper