|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""A pytest reporter that prints one character per test, like Playwright's dot reporter. |
| 16 | +
|
| 17 | +Load it with `-p pytest_dot_reporter` (scripts/ on PYTHONPATH); CI runs the suites |
| 18 | +with it. Each test is one character: `·` passed, `F` failed, `E` failed in a fixture, |
| 19 | +`°` skipped, `x` failed as expected, `X` passed unexpectedly, `×` failed and to be run |
| 20 | +again. Each failure prints in full as soon as it happens, between the dots, so that |
| 21 | +the log of a run that is cut short still has it; the end summary lists the failures |
| 22 | +again, one line each. Unlike the line reporter, it does not need a terminal. |
| 23 | +""" |
| 24 | + |
| 25 | +from typing import Dict, Optional, Tuple |
| 26 | + |
| 27 | +import pytest |
| 28 | +from _pytest.terminal import TerminalReporter |
| 29 | + |
| 30 | +Location = Tuple[str, Optional[int], str] |
| 31 | + |
| 32 | +_LINE_WIDTH = 80 |
| 33 | + |
| 34 | +# The character of each test outcome, and its colors. |
| 35 | +_MARKS: Dict[str, Tuple[str, Dict[str, bool]]] = { |
| 36 | + "passed": ("·", {"green": True}), |
| 37 | + "failed": ("F", {"red": True, "bold": True}), |
| 38 | + "error": ("E", {"red": True, "bold": True}), |
| 39 | + "skipped": ("°", {"yellow": True}), |
| 40 | + "xfailed": ("x", {"yellow": True}), |
| 41 | + "xpassed": ("X", {"yellow": True}), |
| 42 | + "rerun": ("×", {"light": True}), |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +class DotStatus: |
| 47 | + """Takes over the per-test output of the terminal reporter.""" |
| 48 | + |
| 49 | + def __init__(self, reporter: TerminalReporter) -> None: |
| 50 | + self._reporter = reporter |
| 51 | + self._column = 0 |
| 52 | + |
| 53 | + def pytest_runtest_logstart(self, nodeid: str, location: Location) -> None: |
| 54 | + pass |
| 55 | + |
| 56 | + def pytest_runtest_logreport(self, report: pytest.TestReport) -> None: |
| 57 | + reporter = self._reporter |
| 58 | + reporter._tests_ran = True |
| 59 | + category, letter, _ = reporter.config.hook.pytest_report_teststatus( |
| 60 | + report=report, config=reporter.config |
| 61 | + ) |
| 62 | + reporter._add_stats(category, [report]) |
| 63 | + if report.failed or category == "rerun": |
| 64 | + self._print_failure(report, category) |
| 65 | + if not letter: |
| 66 | + # A fixture that ran: the test gets its character from its call. |
| 67 | + return |
| 68 | + if self._column == _LINE_WIDTH: |
| 69 | + reporter._tw.line() |
| 70 | + self._column = 0 |
| 71 | + mark, markup = _MARKS.get(category, (letter[:1], {})) |
| 72 | + reporter._tw.write(mark, **markup) |
| 73 | + reporter._tw.flush() |
| 74 | + self._column += 1 |
| 75 | + # Start the next output of the reporter on a new line. |
| 76 | + reporter.currentfspath = -2 |
| 77 | + |
| 78 | + def pytest_runtest_logfinish(self, nodeid: str, location: Location) -> None: |
| 79 | + pass |
| 80 | + |
| 81 | + def summary_failures(self) -> None: |
| 82 | + # The failures were printed as they happened. |
| 83 | + pass |
| 84 | + |
| 85 | + def summary_errors(self) -> None: |
| 86 | + pass |
| 87 | + |
| 88 | + def _print_failure(self, report: pytest.TestReport, category: str) -> None: |
| 89 | + # As summary_errors() and summary_failures() print it, on a line of its own. |
| 90 | + reporter = self._reporter |
| 91 | + headline = reporter._getfailureheadline(report) |
| 92 | + if category == "rerun": |
| 93 | + headline = f"RERUN {headline}" |
| 94 | + elif report.when != "call": |
| 95 | + headline = f"ERROR at {report.when} of {headline}" |
| 96 | + reporter.write_sep("_", headline, red=True, bold=True) |
| 97 | + reporter._outrep_summary(report) |
| 98 | + reporter._tw.flush() |
| 99 | + self._column = 0 |
| 100 | + |
| 101 | + |
| 102 | +@pytest.hookimpl(trylast=True) |
| 103 | +def pytest_configure(config: pytest.Config) -> None: |
| 104 | + reporter = config.pluginmanager.getplugin("terminalreporter") |
| 105 | + if not isinstance(reporter, TerminalReporter) or hasattr(config, "workerinput"): |
| 106 | + return |
| 107 | + # Keep the reporter object, which xdist already holds. Replace its per-test hooks |
| 108 | + # and its failure sections with DotStatus, then register it again so that pluggy |
| 109 | + # calls the replacements. |
| 110 | + status = DotStatus(reporter) |
| 111 | + config.pluginmanager.unregister(reporter) |
| 112 | + for name in ( |
| 113 | + "pytest_runtest_logstart", |
| 114 | + "pytest_runtest_logreport", |
| 115 | + "pytest_runtest_logfinish", |
| 116 | + "summary_failures", |
| 117 | + "summary_errors", |
| 118 | + ): |
| 119 | + setattr(reporter, name, getattr(status, name)) |
| 120 | + config.pluginmanager.register(reporter, "terminalreporter") |
0 commit comments