unittest needs more flush calls #57445
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee = None closed_at = <Date 2021-12-11.00:36:03.403> created_at = <Date 2011-10-20.19:02:03.573> labels = ['type-bug', 'library', '3.9', '3.10', '3.11'] title = 'unittest needs more flush calls' updated_at = <Date 2021-12-11.00:36:03.402> user = 'https://github.com/petere'
activity = <Date 2021-12-11.00:36:03.402> actor = 'lukasz.langa' assignee = 'none' closed = True closed_date = <Date 2021-12-11.00:36:03.403> closer = 'lukasz.langa' components = ['Library (Lib)'] creation = <Date 2011-10-20.19:02:03.573> creator = 'petere' dependencies =  files = ['23527'] hgrepos =  issue_num = 13236 keywords = ['patch'] message_count = 12.0 messages = ['146043', '146044', '146051', '146446', '229420', '229459', '407338', '407366', '407682', '408268', '408284', '408285'] nosy_count = 10.0 nosy_names = ['pitrou', 'rbcollins', 'ezio.melotti', 'r.david.murray', 'petere', 'lukasz.langa', 'serhiy.storchaka', 'matrixise', 'miss-islington', 'iritkatriel'] pr_nums = ['29864', '29929', '30039'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue13236' versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']
The text was updated successfully, but these errors were encountered:
I'm using the TextTestRunner class in unittest/runner.py with a special file-like object passed in as stream. Doing this loses some output, because the run() method (and some lower-level methods) don't always call flush() on the stream. There is also no obvious cleanup method in the runner class that might do this, so I assume run() should do that itself.
Right now, it looks like it assumes that either the stream is effectively unbuffered, like stderr, or that the end of the program might flush things, but that doesn't always apply.
It looks like the best fix would be a self.stream.flush() call at the end of run(). Another flush() call at the end of printErrorList() would also help.
(In the meantime, I have fixed up my special file-like class to flush its internal buffers when a newline is seen, which kind of works, but a proper cleanup of this matter would still be nice.)
Attached is a test file. The key here is that I'm running the unittest suite inside of a long-running server process, so there is no predictable point of exit and cleanup. Therefore, the steps I show at the end of the file should be run in an interactive interpreter. Here is what I see:
>>> import unittest >>> import testcase >>> unittest.main(module=testcase) .F.
Traceback (most recent call last): File "testcase.py", line 35, in test_three self.fail("intentional failure") AssertionError: intentional failure
Ran 3 tests in 0.002s
>>> import unittest >>> import testcase >>> unittest.main(module=testcase, testRunner=testcase.FunnyTestRunner, exit=False) . F . <unittest.main.TestProgram object at 0xf718fd4c>
You see, the test report at the end is missing.
I'm happy to consider other ways of addressing this, if anyone has an idea.