_testing.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from __future__ import annotations
  2. import types
  3. from abc import ABCMeta, abstractmethod
  4. from collections.abc import AsyncGenerator, Iterable
  5. from typing import Any, Callable, Coroutine, 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 same event
  10. loop.
  11. """
  12. def __enter__(self) -> TestRunner:
  13. return self
  14. def __exit__(
  15. self,
  16. exc_type: type[BaseException] | None,
  17. exc_val: BaseException | None,
  18. exc_tb: types.TracebackType | None,
  19. ) -> bool | None:
  20. self.close()
  21. return None
  22. @abstractmethod
  23. def close(self) -> None:
  24. """Close the event loop."""
  25. @abstractmethod
  26. def run_asyncgen_fixture(
  27. self,
  28. fixture_func: Callable[..., AsyncGenerator[_T, Any]],
  29. kwargs: dict[str, Any],
  30. ) -> Iterable[_T]:
  31. """
  32. Run an async generator fixture.
  33. :param fixture_func: the fixture function
  34. :param kwargs: keyword arguments to call the fixture function with
  35. :return: an iterator yielding the value yielded from the async generator
  36. """
  37. @abstractmethod
  38. def run_fixture(
  39. self,
  40. fixture_func: Callable[..., Coroutine[Any, Any, _T]],
  41. kwargs: dict[str, Any],
  42. ) -> _T:
  43. """
  44. Run an async fixture.
  45. :param fixture_func: the fixture function
  46. :param kwargs: keyword arguments to call the fixture function with
  47. :return: the return value of the fixture function
  48. """
  49. @abstractmethod
  50. def run_test(
  51. self, test_func: Callable[..., Coroutine[Any, Any, Any]], kwargs: dict[str, Any]
  52. ) -> None:
  53. """
  54. Run an async test function.
  55. :param test_func: the test function
  56. :param kwargs: keyword arguments to call the test function with
  57. """