Exemple #1
0
    def summary_warnings(self) -> None:
        if self.hasopt("w"):
            all_warnings = self.stats.get(
                "warnings")  # type: Optional[List[WarningReport]]
            if not all_warnings:
                return

            final = self._already_displayed_warnings is not None
            if final:
                warning_reports = all_warnings[self.
                                               _already_displayed_warnings:]
            else:
                warning_reports = all_warnings
            self._already_displayed_warnings = len(warning_reports)
            if not warning_reports:
                return

            reports_grouped_by_message = (
                order_preserving_dict()
            )  # type: Dict[str, List[WarningReport]]
            for wr in warning_reports:
                reports_grouped_by_message.setdefault(wr.message,
                                                      []).append(wr)

            def collapsed_location_report(reports: List[WarningReport]) -> str:
                locations = []
                for w in reports:
                    location = w.get_location(self.config)
                    if location:
                        locations.append(location)

                if len(locations) < 10:
                    return "\n".join(map(str, locations))

                counts_by_filename = order_preserving_dict(
                )  # type: Dict[str, int]
                for loc in locations:
                    key = str(loc).split("::", 1)[0]
                    counts_by_filename[key] = counts_by_filename.get(key,
                                                                     0) + 1
                return "\n".join(
                    "{}: {} warning{}".format(k, v, "s" if v > 1 else "")
                    for k, v in counts_by_filename.items())

            title = "warnings summary (final)" if final else "warnings summary"
            self.write_sep("=", title, yellow=True, bold=False)
            for message, message_reports in reports_grouped_by_message.items():
                maybe_location = collapsed_location_report(message_reports)
                if maybe_location:
                    self._tw.line(maybe_location)
                    lines = message.splitlines()
                    indented = "\n".join("  " + x for x in lines)
                    message = indented.rstrip()
                else:
                    message = message.rstrip()
                self._tw.line(message)
                self._tw.line()
            self._tw.line(
                "-- Docs: https://docs.pytest.org/en/latest/warnings.html")
Exemple #2
0
    def pytest_collection_modifyitems(
            self, items: List[nodes.Item]) -> Generator[None, None, None]:
        yield

        if self.active:
            new_items = order_preserving_dict()  # type: Dict[str, nodes.Item]
            other_items = order_preserving_dict(
            )  # type: Dict[str, nodes.Item]
            for item in items:
                if item.nodeid not in self.cached_nodeids:
                    new_items[item.nodeid] = item
                else:
                    other_items[item.nodeid] = item

            items[:] = self._get_increasing_order(
                new_items.values()) + self._get_increasing_order(
                    other_items.values())
            self.cached_nodeids.update(new_items)
        else:
            self.cached_nodeids.update(item.nodeid for item in items)
Exemple #3
0
            def collapsed_location_report(reports: List[WarningReport]) -> str:
                locations = []
                for w in reports:
                    location = w.get_location(self.config)
                    if location:
                        locations.append(location)

                if len(locations) < 10:
                    return "\n".join(map(str, locations))

                counts_by_filename = order_preserving_dict()  # type: Dict[str, int]
                for loc in locations:
                    key = str(loc).split("::", 1)[0]
                    counts_by_filename[key] = counts_by_filename.get(key, 0) + 1
                return "\n".join(
                    "{}: {} warning{}".format(k, v, "s" if v > 1 else "")
                    for k, v in counts_by_filename.items()
                )