This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unittest needs more flush calls
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, iritkatriel, lukasz.langa, matrixise, miss-islington, petere, pitrou, r.david.murray, rbcollins, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2011-10-20 19:02 by petere, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
testcase.py petere, 2011-10-26 17:22 test case
Pull Requests
URL Status Linked Edit
PR 29864 merged serhiy.storchaka, 2021-11-30 12:00
PR 29929 merged serhiy.storchaka, 2021-12-05 20:54
PR 30039 merged miss-islington, 2021-12-10 23:36
Messages (12)
msg146043 - (view) Author: Peter Eisentraut (petere) * Date: 2011-10-20 19:02
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.)
msg146044 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-10-20 19:20
How can a lack of flushes lose output?  Delay it maybe, but unless your file class is broken I don't see how data would be lost.
msg146051 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-10-20 21:00
Can you provide a minimal test case to reproduce the issue?
msg146446 - (view) Author: Peter Eisentraut (petere) * Date: 2011-10-26 17:22
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.
======================================================================
FAIL: test_three (testcase.test)
----------------------------------------------------------------------
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

FAILED (failures=1)

That's good.

But:

>>> 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.
msg229420 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2014-10-15 09:09
ping about this bug, do you have the same problem with the last version of Python 3.4 and with Python 2.7.8 ?
msg229459 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-15 15:16
And if the issue still holds, please provide a patch adding the appropriate flush() calls.
msg407338 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-29 23:59
Reproduced on 3.11.
msg407366 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-11-30 12:02
I do not remember details, but I encountered this issue (maybe when run tests in subprocesses?).
msg407682 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-12-04 18:57
New changeset f42a06ba279c916fb67289e47f9bc60dc5dee4ee by Serhiy Storchaka in branch 'main':
bpo-13236: Flush the output stream more often in unittest (GH-29864)
https://github.com/python/cpython/commit/f42a06ba279c916fb67289e47f9bc60dc5dee4ee
msg408268 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-12-10 23:36
New changeset 83fa1291fd0a25216a4a9e990f423682fda67cbe by Serhiy Storchaka in branch '3.10':
[3.10] bpo-13236: Flush the output stream more often in unittest (GH-29864) (GH-29929)
https://github.com/python/cpython/commit/83fa1291fd0a25216a4a9e990f423682fda67cbe
msg408284 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-12-11 00:34
New changeset d55a03e02e69fb7f639998de71ed3f44c2f4f999 by Miss Islington (bot) in branch '3.9':
[3.10] bpo-13236: Flush the output stream more often in unittest (GH-29929) (GH-30039)
https://github.com/python/cpython/commit/d55a03e02e69fb7f639998de71ed3f44c2f4f999
msg408285 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-12-11 00:36
Thanks, Serhiy! ✨ 🍰 ✨
History
Date User Action Args
2022-04-11 14:57:22adminsetgithub: 57445
2021-12-11 00:36:03lukasz.langasetstatus: open -> closed
resolution: fixed
messages: + msg408285

stage: patch review -> resolved
2021-12-11 00:34:50lukasz.langasetmessages: + msg408284
2021-12-10 23:36:24miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request28265
2021-12-10 23:36:19lukasz.langasetnosy: + lukasz.langa
messages: + msg408268
2021-12-05 20:54:52serhiy.storchakasetpull_requests: + pull_request28153
2021-12-04 18:57:27serhiy.storchakasetmessages: + msg407682
2021-11-30 12:02:18serhiy.storchakasetversions: + Python 3.9, Python 3.10
2021-11-30 12:02:06serhiy.storchakasetmessages: + msg407366
2021-11-30 12:00:08serhiy.storchakasetkeywords: + patch
nosy: + serhiy.storchaka

pull_requests: + pull_request28091
stage: test needed -> patch review
2021-11-29 23:59:34iritkatrielsetnosy: + iritkatriel

messages: + msg407338
versions: + Python 3.11, - Python 2.7, Python 3.2
2014-10-16 22:17:38pitrousetnosy: + rbcollins
2014-10-15 15:16:53pitrousetnosy: + pitrou
messages: + msg229459
2014-10-15 09:09:46matrixisesetnosy: + matrixise
messages: + msg229420
2011-10-26 17:22:17peteresetfiles: + testcase.py

messages: + msg146446
2011-10-20 21:00:47ezio.melottisetversions: - Python 3.1
nosy: + ezio.melotti

messages: + msg146051

stage: test needed
2011-10-20 19:20:10r.david.murraysetnosy: + r.david.murray

messages: + msg146044
versions: - Python 2.6
2011-10-20 19:02:03peterecreate