Failed comparisons (__ULTRAPLOT_IMAGE_COUNT__)
+Only failed image comparisons are included in this artifact.
+| Test name | Status | RMS | Tolerance | Images |
|---|
diff --git a/.github/workflows/build-ultraplot.yml b/.github/workflows/build-ultraplot.yml
index 7778a0a5b..e7dc129d9 100644
--- a/.github/workflows/build-ultraplot.yml
+++ b/.github/workflows/build-ultraplot.yml
@@ -299,7 +299,7 @@ jobs:
--mpl \
--mpl-baseline-path=./ultraplot/tests/baseline \
--mpl-results-path=./results/ \
- --mpl-generate-summary=html \
+ --mpl-generate-summary=json \
--mpl-default-style="./ultraplot.yml" \
--junitxml=./results/junit.xml \
"${FILTERED_NODEIDS[@]}"
@@ -333,7 +333,7 @@ jobs:
--mpl \
--mpl-baseline-path=./ultraplot/tests/baseline \
--mpl-results-path=./results/ \
- --mpl-generate-summary=html \
+ --mpl-generate-summary=json \
--mpl-default-style="./ultraplot.yml" \
--junitxml=./results/junit.xml \
ultraplot/tests
@@ -360,11 +360,20 @@ jobs:
exit "$status"
fi
- # Return the html output of the comparison even if failed
+ # Validate the standalone report rendering on each CI environment.
+ - name: Test failed-comparison report generator
+ if: always() && steps.baseline-support.outputs.baseline-supported == 'true'
+ run: python -m unittest discover -s tools/ci -p 'test_collect_failed_comparisons.py'
+
+ # JSON mode does not save images for passing comparisons.
+ - name: Collect failed image comparisons
+ if: always() && steps.baseline-support.outputs.baseline-supported == 'true'
+ run: python tools/ci/collect_failed_comparisons.py --results results --output failed-comparisons
+
- name: Upload comparison failures
if: always()
uses: actions/upload-artifact@v7
with:
name: failed-comparisons-${{ inputs.python-version }}-${{ inputs.matplotlib-version }}-${{ github.sha }}
- path: results/*
+ path: failed-comparisons/
if-no-files-found: ignore
diff --git a/tools/ci/collect_failed_comparisons.py b/tools/ci/collect_failed_comparisons.py
new file mode 100644
index 000000000..0e6cab493
--- /dev/null
+++ b/tools/ci/collect_failed_comparisons.py
@@ -0,0 +1,197 @@
+"""Collect failed pytest-mpl comparisons into a small HTML artifact.
+
+Use --mpl-generate-summary=json, not HTML, when running pytest-mpl. Its HTML
+option implicitly enables --mpl-results-always and saves passing images.
+"""
+
+from __future__ import annotations
+
+import argparse
+import html
+import json
+import platform
+import shutil
+import xml.etree.ElementTree as ET
+from datetime import datetime, timezone
+from importlib.metadata import PackageNotFoundError, version
+from pathlib import Path
+from urllib.parse import quote
+
+_IMAGE_FIELDS = (
+ ("baseline_image", "Baseline"),
+ ("result_image", "Actual"),
+ ("diff_image", "Difference"),
+)
+
+
+def junit_report(path: Path) -> tuple[dict[str, int] | None, list[str]]:
+ """Return counts for the whole pytest run, excluding skipped tests."""
+ if not path.is_file():
+ return None, []
+
+ try:
+ root = ET.parse(path).getroot()
+ except ET.ParseError:
+ # A cancelled test run might leave a truncated JUnit XML file.
+ return None, []
+
+ counts = {"passed": 0, "failed": 0, "errors": 0}
+ failures = []
+ for case in root.iter("testcase"):
+ if case.find("error") is not None:
+ status = "errors"
+ elif case.find("failure") is not None:
+ status = "failed"
+ elif case.find("skipped") is not None:
+ continue
+ else:
+ status = "passed"
+ counts[status] += 1
+ if status != "passed":
+ failures.append(
+ f"{case.get('classname', '')}.{case.get('name', '')}".lstrip(".")
+ )
+ return counts, failures
+
+
+def collect(results_dir: Path, output_dir: Path) -> int:
+ """Copy failed image comparisons and return their count."""
+ results_dir = results_dir.resolve()
+ output_dir = output_dir.resolve()
+ if output_dir == results_dir or results_dir in output_dir.parents:
+ raise ValueError("Artifact output must not be inside the pytest-mpl results directory")
+
+ summary = results_dir / "results.json"
+ results = json.loads(summary.read_text(encoding="utf-8")) if summary.is_file() else {}
+ if not isinstance(results, dict):
+ raise ValueError("Unexpected pytest-mpl JSON summary format")
+
+ failed = {name: item for name, item in results.items() if item.get("status") == "failed"}
+ junit = results_dir / "junit.xml"
+ counts, junit_failures = junit_report(junit)
+
+ if not failed and not junit_failures:
+ print("No failed tests; no artifact to upload.")
+ return 0
+
+ if counts is None:
+ # The fallback covers image tests only; full-suite counts require JUnit.
+ counts = {
+ "passed": sum(item.get("status") == "passed" for item in results.values()),
+ "failed": len(failed),
+ "errors": 0,
+ }
+
+ output_dir.mkdir(parents=True, exist_ok=True)
+ if junit.is_file():
+ shutil.copy2(junit, output_dir / "junit.xml")
+ (output_dir / "results.json").write_text(
+ json.dumps(failed, indent=2) + "\n", encoding="utf-8"
+ )
+
+ rows = []
+ for index, (name, item) in enumerate(sorted(failed.items())):
+ images = []
+ for field, label in _IMAGE_FIELDS:
+ raw_path = item.get(field)
+ if not raw_path:
+ images.append(
+ f'
{diagnostic}'
+ '{html.escape(name)}pytest-mpl comparison results
+Only failed image comparisons are included in this artifact.
+| Test name | Status | RMS | Tolerance | Images |
|---|