_testing.py 2.1 KB

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