pytester_assertions.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Helper plugin for pytester; should not be loaded on its own."""
  2. # This plugin contains assertions used by pytester. pytester cannot
  3. # contain them itself, since it is imported by the `pytest` module,
  4. # hence cannot be subject to assertion rewriting, which requires a
  5. # module to not be already imported.
  6. from __future__ import annotations
  7. from collections.abc import Sequence
  8. from _pytest.reports import CollectReport
  9. from _pytest.reports import TestReport
  10. def assertoutcome(
  11. outcomes: tuple[
  12. Sequence[TestReport],
  13. Sequence[CollectReport | TestReport],
  14. Sequence[CollectReport | TestReport],
  15. ],
  16. passed: int = 0,
  17. skipped: int = 0,
  18. failed: int = 0,
  19. ) -> None:
  20. __tracebackhide__ = True
  21. realpassed, realskipped, realfailed = outcomes
  22. obtained = {
  23. "passed": len(realpassed),
  24. "skipped": len(realskipped),
  25. "failed": len(realfailed),
  26. }
  27. expected = {"passed": passed, "skipped": skipped, "failed": failed}
  28. assert obtained == expected, outcomes
  29. def assert_outcomes(
  30. outcomes: dict[str, int],
  31. passed: int = 0,
  32. skipped: int = 0,
  33. failed: int = 0,
  34. errors: int = 0,
  35. xpassed: int = 0,
  36. xfailed: int = 0,
  37. warnings: int | None = None,
  38. deselected: int | None = None,
  39. ) -> None:
  40. """Assert that the specified outcomes appear with the respective
  41. numbers (0 means it didn't occur) in the text output from a test run."""
  42. __tracebackhide__ = True
  43. obtained = {
  44. "passed": outcomes.get("passed", 0),
  45. "skipped": outcomes.get("skipped", 0),
  46. "failed": outcomes.get("failed", 0),
  47. "errors": outcomes.get("errors", 0),
  48. "xpassed": outcomes.get("xpassed", 0),
  49. "xfailed": outcomes.get("xfailed", 0),
  50. }
  51. expected = {
  52. "passed": passed,
  53. "skipped": skipped,
  54. "failed": failed,
  55. "errors": errors,
  56. "xpassed": xpassed,
  57. "xfailed": xfailed,
  58. }
  59. if warnings is not None:
  60. obtained["warnings"] = outcomes.get("warnings", 0)
  61. expected["warnings"] = warnings
  62. if deselected is not None:
  63. obtained["deselected"] = outcomes.get("deselected", 0)
  64. expected["deselected"] = deselected
  65. assert obtained == expected