pytester_assertions.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 typing import Dict
  7. from typing import Optional
  8. from typing import Sequence
  9. from typing import Tuple
  10. from typing import Union
  11. from _pytest.reports import CollectReport
  12. from _pytest.reports import TestReport
  13. def assertoutcome(
  14. outcomes: Tuple[
  15. Sequence[TestReport],
  16. Sequence[Union[CollectReport, TestReport]],
  17. Sequence[Union[CollectReport, TestReport]],
  18. ],
  19. passed: int = 0,
  20. skipped: int = 0,
  21. failed: int = 0,
  22. ) -> None:
  23. __tracebackhide__ = True
  24. realpassed, realskipped, realfailed = outcomes
  25. obtained = {
  26. "passed": len(realpassed),
  27. "skipped": len(realskipped),
  28. "failed": len(realfailed),
  29. }
  30. expected = {"passed": passed, "skipped": skipped, "failed": failed}
  31. assert obtained == expected, outcomes
  32. def assert_outcomes(
  33. outcomes: Dict[str, int],
  34. passed: int = 0,
  35. skipped: int = 0,
  36. failed: int = 0,
  37. errors: int = 0,
  38. xpassed: int = 0,
  39. xfailed: int = 0,
  40. warnings: Optional[int] = None,
  41. deselected: Optional[int] = None,
  42. ) -> None:
  43. """Assert that the specified outcomes appear with the respective
  44. numbers (0 means it didn't occur) in the text output from a test run."""
  45. __tracebackhide__ = True
  46. obtained = {
  47. "passed": outcomes.get("passed", 0),
  48. "skipped": outcomes.get("skipped", 0),
  49. "failed": outcomes.get("failed", 0),
  50. "errors": outcomes.get("errors", 0),
  51. "xpassed": outcomes.get("xpassed", 0),
  52. "xfailed": outcomes.get("xfailed", 0),
  53. }
  54. expected = {
  55. "passed": passed,
  56. "skipped": skipped,
  57. "failed": failed,
  58. "errors": errors,
  59. "xpassed": xpassed,
  60. "xfailed": xfailed,
  61. }
  62. if warnings is not None:
  63. obtained["warnings"] = outcomes.get("warnings", 0)
  64. expected["warnings"] = warnings
  65. if deselected is not None:
  66. obtained["deselected"] = outcomes.get("deselected", 0)
  67. expected["deselected"] = deselected
  68. assert obtained == expected