_testing.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from __future__ import annotations
  2. import types
  3. from abc import ABCMeta, abstractmethod
  4. from collections.abc import AsyncGenerator, Callable, Coroutine, Iterable
  5. from typing import Any, TypeVar
  6. _T = TypeVar("_T")
  7. class TestRunner(metaclass=ABCMeta):
  8. """
  9. Encapsulates a running event loop. Every call made through this object will use the
  10. same event loop.
  11. """
  12. def __enter__(self) -> TestRunner:
  13. return self
  14. @abstractmethod
  15. def __exit__(
  16. self,
  17. exc_type: type[BaseException] | None,
  18. exc_val: BaseException | None,
  19. exc_tb: types.TracebackType | None,
  20. ) -> bool | None: ...
  21. @abstractmethod
  22. def run_asyncgen_fixture(
  23. self,
  24. fixture_func: Callable[..., AsyncGenerator[_T, Any]],
  25. kwargs: dict[str, Any],
  26. ) -> Iterable[_T]:
  27. """
  28. Run an async generator fixture.
  29. :param fixture_func: the fixture function
  30. :param kwargs: keyword arguments to call the fixture function with
  31. :return: an iterator yielding the value yielded from the async generator
  32. """
  33. @abstractmethod
  34. def run_fixture(
  35. self,
  36. fixture_func: Callable[..., Coroutine[Any, Any, _T]],
  37. kwargs: dict[str, Any],
  38. ) -> _T:
  39. """
  40. Run an async fixture.
  41. :param fixture_func: the fixture function
  42. :param kwargs: keyword arguments to call the fixture function with
  43. :return: the return value of the fixture function
  44. """
  45. @abstractmethod
  46. def run_test(
  47. self, test_func: Callable[..., Coroutine[Any, Any, Any]], kwargs: dict[str, Any]
  48. ) -> None:
  49. """
  50. Run an async test function.
  51. :param test_func: the test function
  52. :param kwargs: keyword arguments to call the test function with
  53. """
  54. @abstractmethod
  55. def is_running(self) -> bool:
  56. """
  57. Check if the test runner is running.
  58. :return: ``True`` if the coroutine is currently being run, ``False`` otherwise.
  59. """