-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unittest needs more flush calls #57445
Comments
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.) |
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. |
Can you provide a minimal test case to reproduce the issue? |
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 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. |
ping about this bug, do you have the same problem with the last version of Python 3.4 and with Python 2.7.8 ? |
And if the issue still holds, please provide a patch adding the appropriate flush() calls. |
Reproduced on 3.11. |
I do not remember details, but I encountered this issue (maybe when run tests in subprocesses?). |
Thanks, Serhiy! ✨ 🍰 ✨ |
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
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: