Inconsistent wrapping of sys.stdout/stderr causes both capfd and capsys to fail #9361
Labels
plugin: capture
related to the capture builtin plugin
type: enhancement
new feature or API change, should be merged into features branch
If stdout or stderr objects are imported from sys,
capsys
andcapfd
cannot wrap them and thus nothing is captured. I would still expect to be able to capture them oncapfd
but because pytest itself still manages to wrap them (prior to loading the test module I presume), nothing is actually written to these FDs.Running this, the first one fails because nothing is captured. Adding
os.system("echo Hello")
would make this test pass, as true-fd stdout output is still captured. The second test passes, presumably becausecapfd
then also wraps the sys.stdout object so that it can receive anything written there.Pytest still prints on console the correct capture, showing that pytest itself successfully wrapped the stdout object:
Running pytest 6.2.5 on Linux. Adding
--capture=tee-sys
allows both tests to pass. This bug is somewhat related to #8900.Since pytest itself has priority access to do its wrapping prior to loading any user code, it would be nice if
capsys
andcapfd
could also inject their code to the already provided wrapper, avoiding the error-prone rewrapping. This would allow both of them work as expected irregardless of how the application being tested imports the sys module.The text was updated successfully, but these errors were encountered: