|
43 | 43 |
|
44 | 44 | import pytest |
45 | 45 |
|
46 | | -from pytest_mpl.summary.html import generate_summary_html |
| 46 | +from pytest_mpl.summary.html import generate_summary_html, generate_summary_basic_html |
47 | 47 |
|
48 | 48 | SUPPORTED_FORMATS = {'html', 'json', 'basic-html'} |
49 | 49 |
|
|
53 | 53 | Actual shape: {actual_shape} |
54 | 54 | {actual_path}""" |
55 | 55 |
|
56 | | -HTML_INTRO = """ |
57 | | -<!DOCTYPE html> |
58 | | -<html> |
59 | | -<head> |
60 | | -<style> |
61 | | -table, th, td { |
62 | | - border: 1px solid black; |
63 | | -} |
64 | | -.summary > div { |
65 | | - padding: 0.5em; |
66 | | -} |
67 | | -tr.passed .status, .rms.passed, .hashes.passed { |
68 | | - color: green; |
69 | | -} |
70 | | -tr.failed .status, .rms.failed, .hashes.failed { |
71 | | - color: red; |
72 | | -} |
73 | | -</style> |
74 | | -</head> |
75 | | -<body> |
76 | | -<h2>Image test comparison</h2> |
77 | | -%summary% |
78 | | -<table> |
79 | | - <tr> |
80 | | - <th>Test Name</th> |
81 | | - <th>Baseline image</th> |
82 | | - <th>Diff</th> |
83 | | - <th>New image</th> |
84 | | - </tr> |
85 | | -""" |
86 | | - |
87 | 56 |
|
88 | 57 | def _download_file(baseline, filename): |
89 | 58 | # Note that baseline can be a comma-separated list of URLs that we can |
@@ -714,105 +683,6 @@ def item_function_wrapper(*args, **kwargs): |
714 | 683 | else: |
715 | 684 | item.obj = item_function_wrapper |
716 | 685 |
|
717 | | - def generate_stats(self): |
718 | | - """ |
719 | | - Generate a dictionary of summary statistics. |
720 | | - """ |
721 | | - stats = {'passed': 0, 'failed': 0, 'passed_baseline': 0, 'failed_baseline': 0, 'skipped': 0} |
722 | | - for test in self._test_results.values(): |
723 | | - if test['status'] == 'passed': |
724 | | - stats['passed'] += 1 |
725 | | - if test['rms'] is not None: |
726 | | - stats['failed_baseline'] += 1 |
727 | | - elif test['status'] == 'failed': |
728 | | - stats['failed'] += 1 |
729 | | - if test['rms'] is None: |
730 | | - stats['passed_baseline'] += 1 |
731 | | - elif test['status'] == 'skipped': |
732 | | - stats['skipped'] += 1 |
733 | | - else: |
734 | | - raise ValueError(f"Unknown test status '{test['status']}'.") |
735 | | - self._test_stats = stats |
736 | | - |
737 | | - def generate_summary_basic_html(self): |
738 | | - """ |
739 | | - Generate a simple HTML table of the failed test results |
740 | | - """ |
741 | | - html_file = self.results_dir / 'fig_comparison_basic.html' |
742 | | - with open(html_file, 'w') as f: |
743 | | - |
744 | | - passed = f"{self._test_stats['passed']} passed" |
745 | | - if self._test_stats['failed_baseline'] > 0: |
746 | | - passed += (" hash comparison, although " |
747 | | - f"{self._test_stats['failed_baseline']} " |
748 | | - "of those have a different baseline image") |
749 | | - |
750 | | - failed = f"{self._test_stats['failed']} failed" |
751 | | - if self._test_stats['passed_baseline'] > 0: |
752 | | - failed += (" hash comparison, although " |
753 | | - f"{self._test_stats['passed_baseline']} " |
754 | | - "of those have a matching baseline image") |
755 | | - |
756 | | - f.write(HTML_INTRO.replace('%summary%', f'<p>{passed}.</p><p>{failed}.</p>')) |
757 | | - |
758 | | - for test_name in sorted(self._test_results.keys()): |
759 | | - summary = self._test_results[test_name] |
760 | | - |
761 | | - if not self.results_always and summary['result_image'] is None: |
762 | | - continue # Don't show test if no result image |
763 | | - |
764 | | - if summary['rms'] is None and summary['tolerance'] is not None: |
765 | | - rms = (f'<div class="rms passed">\n' |
766 | | - f' <strong>RMS:</strong> ' |
767 | | - f' < <span class="tolerance">{summary["tolerance"]}</span>\n' |
768 | | - f'</div>') |
769 | | - elif summary['rms'] is not None: |
770 | | - rms = (f'<div class="rms failed">\n' |
771 | | - f' <strong>RMS:</strong> ' |
772 | | - f' <span class="rms">{summary["rms"]}</span>\n' |
773 | | - f'</div>') |
774 | | - else: |
775 | | - rms = '' |
776 | | - |
777 | | - hashes = '' |
778 | | - if summary['baseline_hash'] is not None: |
779 | | - hashes += (f' <div class="baseline">Baseline: ' |
780 | | - f'{summary["baseline_hash"]}</div>\n') |
781 | | - if summary['result_hash'] is not None: |
782 | | - hashes += (f' <div class="result">Result: ' |
783 | | - f'{summary["result_hash"]}</div>\n') |
784 | | - if len(hashes) > 0: |
785 | | - if summary["baseline_hash"] == summary["result_hash"]: |
786 | | - hash_result = 'passed' |
787 | | - else: |
788 | | - hash_result = 'failed' |
789 | | - hashes = f'<div class="hashes {hash_result}">\n{hashes}</div>' |
790 | | - |
791 | | - images = {} |
792 | | - for image_type in ['baseline_image', 'diff_image', 'result_image']: |
793 | | - if summary[image_type] is not None: |
794 | | - images[image_type] = f'<img src="{summary[image_type]}" />' |
795 | | - else: |
796 | | - images[image_type] = '' |
797 | | - |
798 | | - f.write(f'<tr class="{summary["status"]}">\n' |
799 | | - ' <td>\n' |
800 | | - ' <div class="summary">\n' |
801 | | - f' <div class="test-name">{test_name}</div>\n' |
802 | | - f' <div class="status">{summary["status"]}</div>\n' |
803 | | - f' {rms}{hashes}\n' |
804 | | - ' </td>\n' |
805 | | - f' <td>{images["baseline_image"]}</td>\n' |
806 | | - f' <td>{images["diff_image"]}</td>\n' |
807 | | - f' <td>{images["result_image"]}</td>\n' |
808 | | - '</tr>\n\n') |
809 | | - |
810 | | - f.write('</table>\n') |
811 | | - f.write('</body>\n') |
812 | | - f.write('</html>') |
813 | | - |
814 | | - return html_file |
815 | | - |
816 | 686 | def generate_summary_json(self): |
817 | 687 | json_file = self.results_dir / 'results.json' |
818 | 688 | with open(json_file, 'w') as f: |
@@ -845,16 +715,14 @@ def pytest_unconfigure(self, config): |
845 | 715 | if self._test_results[test_name][image_type] == '%EXISTS%': |
846 | 716 | self._test_results[test_name][image_type] = str(directory / filename) |
847 | 717 |
|
848 | | - self.generate_stats() |
849 | | - |
850 | 718 | if 'json' in self.generate_summary: |
851 | 719 | summary = self.generate_summary_json() |
852 | 720 | print(f"A JSON report can be found at: {summary}") |
853 | 721 | if 'html' in self.generate_summary: |
854 | 722 | summary = generate_summary_html(self._test_results, self.results_dir) |
855 | 723 | print(f"A summary of the failed tests can be found at: {summary}") |
856 | 724 | if 'basic-html' in self.generate_summary: |
857 | | - summary = self.generate_summary_basic_html() |
| 725 | + summary = generate_summary_basic_html(self._test_results, self.results_dir) |
858 | 726 | print(f"A summary of the failed tests can be found at: {summary}") |
859 | 727 |
|
860 | 728 |
|
|
0 commit comments