-
Notifications
You must be signed in to change notification settings - Fork 132
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
Test that has child process logging to stdout not consistent when run with python -m unittest vs nose2 #496
Comments
Thanks for reporting this and providing a reproduction of the issue; I can confirm that this isn't behaving correctly under nose2 and I've produced a smaller reproduction to try to narrow this down more. We don't need the indirection/wrapper to get this to fail, this also doesn't work: import logging
import os
import sys
import unittest
from multiprocessing import Process
def child(stdout_file):
old_stdout = sys.stdout
with open(stdout_file, "w") as fp:
try:
sys.stdout = fp
logging.basicConfig(
handlers=[logging.StreamHandler(sys.stdout)],
level=logging.INFO,
format="%(message)s",
datefmt="%Y-%m-%dT%H:%M:%S%z",
)
logging.info("MY LOG MESSAGE")
finally:
sys.stdout = old_stdout
class TestLoggingInChildProc(unittest.TestCase):
def test_logging_in_child_proc(self):
stdout = os.path.join(os.path.dirname(__file__), "stdout.txt")
p = Process(target=child, args=[stdout])
p.start()
p.join()
p.close()
with open(stdout) as fp:
self.assertEqual("MY LOG MESSAGE", fp.read().strip()) I also couldn't find a plugin responsible for the failure. e.g. This fails: I'll look into this as I'm able, but there's no very obvious place I can point at as the cause. Given that this doesn't work even on very old nose2 versions (dating all the way back to |
I have a command line command that I'm trying to write an end-to-end test for to make sure everything is working well. I wanted to invoke the CLI via a
multiprocessing.Process
, because when the CLI starts running it configures the root logger, and I want to test that, along with everything else, without creating inconsistencies in my test suite. Below is a simple of the type of test I'm interested in doing. You can see that I havechild
, which is the entrypoint of my CLI, you can see that it configures logging, and also that it makes a couple of print statements. The test itself verifies both the print statement and the log message show up in stdout, which is redirected to a file in a child process.When I run this test with unittests, everything passes. When I run with nose2, I get a failure. See below for details.
tests/test_example.py
When I run
tree .
, I see:When I run
python -m unittest tests.test_example
the test passes, but when I runnose2 test_example
the test fails. Here is the failure text:When I invoke this test using
python -m unittest test_example.py
the test passes. However, when I invoke this script usingnose2
The text was updated successfully, but these errors were encountered: