|
1 |
| -import unittest |
2 | 1 | import inspect
|
| 2 | +import sys |
| 3 | +import unittest |
3 | 4 |
|
4 |
| -from .module import SnapshotModule, SnapshotTest |
5 | 5 | from .diff import PrettyDiff
|
6 |
| -from .reporting import diff_report |
| 6 | +from .module import SnapshotModule, SnapshotTest |
| 7 | +from .reporting import diff_report, reporting_lines |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class UnitTestSnapshotTest(SnapshotTest):
|
@@ -36,6 +37,10 @@ def test_name(self):
|
36 | 37 | # Inspired by https://gist.github.com/twolfson/13f5f5784f67fd49b245
|
37 | 38 | class TestCase(unittest.TestCase):
|
38 | 39 |
|
| 40 | + # Whether snapshots should be updated, for all unittest-derived frameworks. |
| 41 | + # Set (perhaps circuitously) in runner init from the --snapshot-update |
| 42 | + # command line option. (.unittest.TestCase.snapshot_should_update is the |
| 43 | + # equivalent of pytest's config.option.snapshot_update.) |
39 | 44 | snapshot_should_update = False
|
40 | 45 |
|
41 | 46 | @classmethod
|
@@ -99,3 +104,140 @@ def assert_match_snapshot(self, value, name=""):
|
99 | 104 | self._snapshot.assert_match(value, name=name)
|
100 | 105 |
|
101 | 106 | assertMatchSnapshot = assert_match_snapshot
|
| 107 | + |
| 108 | + |
| 109 | +def output_snapshottest_summary(stream=None, testing_cli=None): |
| 110 | + """ |
| 111 | + Outputs a summary of snapshot tests for the session, if any. |
| 112 | +
|
| 113 | + Call at the end of a test session to write results summary |
| 114 | + to stream (default sys.stderr). If no snapshot tests were run, |
| 115 | + outputs nothing. |
| 116 | +
|
| 117 | + testing_cli (default from sys.argv) should be the string command |
| 118 | + line that invokes the tests, and is used to explain how to update |
| 119 | + snapshots. |
| 120 | +
|
| 121 | + (This is the equivalent of .pytest.SnapshotSession.display, |
| 122 | + for unittest-derived frameworks.) |
| 123 | + """ |
| 124 | + # TODO: Call this to replace near-duplicate code in .django and .nose. |
| 125 | + |
| 126 | + if not SnapshotModule.has_snapshots(): |
| 127 | + return |
| 128 | + |
| 129 | + if stream is None: |
| 130 | + # This follows unittest.TextTestRunner, which by default uses sys.stderr |
| 131 | + # for test status and summary info (not sys.stdout). |
| 132 | + stream = sys.stderr |
| 133 | + if testing_cli is None: |
| 134 | + # We can't really recover the exact command line formatted for the user's shell |
| 135 | + # (quoting, etc.), but this should be close enough to get the point across. |
| 136 | + testing_cli = " ".join(sys.argv) |
| 137 | + |
| 138 | + separator1 = "=" * 70 |
| 139 | + separator2 = "-" * 70 |
| 140 | + |
| 141 | + print(separator1, file=stream) |
| 142 | + print("SnapshotTest summary", file=stream) |
| 143 | + print(separator2, file=stream) |
| 144 | + for line in reporting_lines(testing_cli): |
| 145 | + print(line, file=stream) |
| 146 | + print(separator1, file=stream) |
| 147 | + |
| 148 | + |
| 149 | +def finalize_snapshots(): |
| 150 | + """ |
| 151 | + Call at the end of a unittest session to delete unused snapshots. |
| 152 | +
|
| 153 | + (This deletes the data needed for SnapshotModule.total_unvisited_snapshots. |
| 154 | + Complete any reporting before calling this function.) |
| 155 | + """ |
| 156 | + # TODO: this is duplicated in four places (with varying "should_update" conditions). |
| 157 | + # Move it into shared code for snapshot sessions (which is currently implemented |
| 158 | + # as classmethods on SnapshotModule). |
| 159 | + if TestCase.snapshot_should_update: |
| 160 | + for module in SnapshotModule.get_modules(): |
| 161 | + module.delete_unvisited() |
| 162 | + module.save() |
| 163 | + |
| 164 | + |
| 165 | +class SnapshotTestRunnerMixin: |
| 166 | + """ |
| 167 | + A mixin for a unittest TestRunner that adds snapshottest session handling. |
| 168 | +
|
| 169 | + Note: a TestRunner is not responsible for command line options. If you are |
| 170 | + adding snapshottest support to other unittest-derived frameworks, you must |
| 171 | + also arrange to set snapshottest.unittest.TestCase.snapshot_should_update |
| 172 | + when the user requests --snapshot-update. |
| 173 | + """ |
| 174 | + |
| 175 | + def run(self, test): |
| 176 | + result = super().run(test) |
| 177 | + self.report_snapshottest_summary() |
| 178 | + finalize_snapshots() |
| 179 | + return result |
| 180 | + |
| 181 | + def report_snapshottest_summary(self): |
| 182 | + """Report a summary of snapshottest results for the session""" |
| 183 | + if hasattr(self, "stream"): |
| 184 | + # Mixed into a unittest.TextTestRunner or similar (with an output stream) |
| 185 | + output_snapshottest_summary(self.stream) |
| 186 | + else: |
| 187 | + # Mixed into some sort of graphical frontend, probably |
| 188 | + raise NotImplementedError( |
| 189 | + "Non-text TestRunner with SnapshotTestRunnerMixin" |
| 190 | + " must implement report_snapshottest_summary" |
| 191 | + ) |
| 192 | + |
| 193 | + |
| 194 | +class SnapshotTextTestRunner(SnapshotTestRunnerMixin, unittest.TextTestRunner): |
| 195 | + """ |
| 196 | + Version of unittest.TextTestRunner that adds snapshottest session handling. |
| 197 | + """ |
| 198 | + |
| 199 | + pass |
| 200 | + |
| 201 | + |
| 202 | +class SnapshotTestProgram(unittest.TestProgram): |
| 203 | + """ |
| 204 | + Augmented implementation of unittest.main that adds --snapshot-update |
| 205 | + command line option, and that ensures testRunner includes snapshottest |
| 206 | + session handling. |
| 207 | + """ |
| 208 | + |
| 209 | + def __init__(self, *args, testRunner=None, **kwargs): |
| 210 | + # (For simplicity, we only allow testRunner as a kwarg.) |
| 211 | + if testRunner is None: |
| 212 | + testRunner = SnapshotTextTestRunner |
| 213 | + # Verify the testRunner includes snapshot session handling. |
| 214 | + # "The testRunner argument can either be a test runner class |
| 215 | + # or an already created instance of it." |
| 216 | + if not issubclass(testRunner, SnapshotTestRunnerMixin) and not isinstance( |
| 217 | + testRunner, SnapshotTestRunnerMixin |
| 218 | + ): |
| 219 | + raise TypeError( |
| 220 | + "snapshottest testRunner must include SnapshotTestRunnerMixin" |
| 221 | + ) |
| 222 | + |
| 223 | + self._snapshot_update = False |
| 224 | + super().__init__(*args, testRunner=testRunner, **kwargs) |
| 225 | + |
| 226 | + def _getParentArgParser(self): |
| 227 | + # (Yes, this is hooking a private method. Sorry. |
| 228 | + # unittest.TestProgram isn't really designed to be extended.) |
| 229 | + parser = super()._getParentArgParser() |
| 230 | + parser.add_argument( |
| 231 | + "--snapshot-update", |
| 232 | + dest="_snapshot_update", |
| 233 | + action="store_true", |
| 234 | + help="Update snapshottest snapshots", |
| 235 | + ) |
| 236 | + return parser |
| 237 | + |
| 238 | + def runTests(self): |
| 239 | + TestCase.snapshot_should_update = self._snapshot_update |
| 240 | + super().runTests() |
| 241 | + |
| 242 | + |
| 243 | +main = SnapshotTestProgram |
0 commit comments