Skip to content

Commit 1944434

Browse files
bpo-22815: Print unexpected successes in summary in TextTestResult (pythonGH-30138)
1 parent a23ab7b commit 1944434

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

Lib/unittest/runner.py

+6
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ def printErrors(self):
142142
self.stream.flush()
143143
self.printErrorList('ERROR', self.errors)
144144
self.printErrorList('FAIL', self.failures)
145+
unexpectedSuccesses = getattr(self, 'unexpectedSuccesses', ())
146+
if unexpectedSuccesses:
147+
self.stream.writeln(self.separator1)
148+
for test in unexpectedSuccesses:
149+
self.stream.writeln(f"UNEXPECTED SUCCESS: {self.getDescription(test)}")
150+
self.stream.flush()
145151

146152
def printErrorList(self, flavour, errors):
147153
for test, err in errors:

Lib/unittest/test/test_program.py

+32-8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ def testPass(self):
6161
pass
6262
def testFail(self):
6363
raise AssertionError
64+
def testError(self):
65+
1/0
66+
@unittest.skip('skipping')
67+
def testSkipped(self):
68+
raise AssertionError
69+
@unittest.expectedFailure
70+
def testExpectedFailure(self):
71+
raise AssertionError
72+
@unittest.expectedFailure
73+
def testUnexpectedSuccess(self):
74+
pass
6475

6576
class FooBarLoader(unittest.TestLoader):
6677
"""Test loader that returns a suite containing FooBar."""
@@ -111,9 +122,13 @@ def test_NonExit(self):
111122
testRunner=unittest.TextTestRunner(stream=stream),
112123
testLoader=self.FooBarLoader())
113124
self.assertTrue(hasattr(program, 'result'))
114-
self.assertIn('\nFAIL: testFail ', stream.getvalue())
115-
self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
116-
125+
out = stream.getvalue()
126+
self.assertIn('\nFAIL: testFail ', out)
127+
self.assertIn('\nERROR: testError ', out)
128+
self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
129+
expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
130+
'expected failures=1, unexpected successes=1)\n')
131+
self.assertTrue(out.endswith(expected))
117132

118133
def test_Exit(self):
119134
stream = BufferedWriter()
@@ -124,9 +139,13 @@ def test_Exit(self):
124139
testRunner=unittest.TextTestRunner(stream=stream),
125140
exit=True,
126141
testLoader=self.FooBarLoader())
127-
self.assertIn('\nFAIL: testFail ', stream.getvalue())
128-
self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
129-
142+
out = stream.getvalue()
143+
self.assertIn('\nFAIL: testFail ', out)
144+
self.assertIn('\nERROR: testError ', out)
145+
self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
146+
expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
147+
'expected failures=1, unexpected successes=1)\n')
148+
self.assertTrue(out.endswith(expected))
130149

131150
def test_ExitAsDefault(self):
132151
stream = BufferedWriter()
@@ -136,8 +155,13 @@ def test_ExitAsDefault(self):
136155
argv=["foobar"],
137156
testRunner=unittest.TextTestRunner(stream=stream),
138157
testLoader=self.FooBarLoader())
139-
self.assertIn('\nFAIL: testFail ', stream.getvalue())
140-
self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
158+
out = stream.getvalue()
159+
self.assertIn('\nFAIL: testFail ', out)
160+
self.assertIn('\nERROR: testError ', out)
161+
self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
162+
expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
163+
'expected failures=1, unexpected successes=1)\n')
164+
self.assertTrue(out.endswith(expected))
141165

142166

143167
class InitialisableProgram(unittest.TestProgram):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Print unexpected successes together with failures and errors in summary in
2+
:class:`unittest.TextTestResult`.

0 commit comments

Comments
 (0)