Skip to content
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

capture: Output in "Captured stdout" section but not available to test #6676

Closed
shepherdjay opened this issue Feb 5, 2020 · 6 comments
Closed
Labels
plugin: capture related to the capture builtin plugin type: question general question, might be closed after 2 weeks of inactivity

Comments

@shepherdjay
Copy link

I believe this may be the same issue as #5997 but in case not opened as a separate issue. When a module passes sys.stdout to the print function's file parameter the output says it is captured but is not available to the test as an assertion.

Minimal Code:

File: helloworld.py

from sys import stdout

def main():
    print("Hello World", file=stdout)

if __name__ == '__main__':
    main()

File: test_helloworld.py

from helloworld import main as worldmain

def test_helloworld(capsys):
    worldmain()
    assert capsys.readouterr().out == "Hello World"

Test Results

======================================================================================================================================================================= test session starts ========================================================================================================================================================================
platform win32 -- Python 3.6.5, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: C:\xxxx\nagiospluginexample
collected 1 item                                                                                                                                                                                                                                                                                                                                                    

test_helloworld.py F                                                                                                                                                                                                                                                                                                                                          [100%]

============================================================================================================================================================================= FAILURES =============================================================================================================================================================================
_________________________________________________________________________________________________________________________________________________________________________ test_helloworld __________________________________________________________________________________________________________________________________________________________________________

capsys = <_pytest.capture.CaptureFixture object at 0x000002976687A1D0>

    def test_helloworld(capsys):
        worldmain()
>       assert capsys.readouterr().out == "Hello World"
E       AssertionError: assert '' == 'Hello World'
E         + Hello World

test_helloworld.py:5: AssertionError
----------------------------------------------------------------------------------------------------------------------------------------------------------------------- Captured stdout call -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hello World
======================================================================================================================================================================== 1 failed in 0.05s =========================================================================================================================================================================

PIP List

Package            Version
------------------ -------
atomicwrites       1.3.0
attrs              19.3.0
colorama           0.4.3
importlib-metadata 1.5.0
more-itertools     8.2.0
packaging          20.1
pip                19.0.3
pluggy             0.13.1
py                 1.8.1
pyparsing          2.4.6
pytest             5.3.5
setuptools         40.8.0
six                1.14.0
wcwidth            0.1.8
zipp               2.1.0
@RonnyPfannschmidt
Copy link
Member

that's a user error - the module takes a different file object to the default
when capture setup happens, the object is already the different one

@RonnyPfannschmidt RonnyPfannschmidt added plugin: capture related to the capture builtin plugin type: question general question, might be closed after 2 weeks of inactivity labels Feb 5, 2020
@shepherdjay
Copy link
Author

So this is a small example to demonstrate the issue but the problem I’m experiencing is with a larger library that makes use of this style of passing sys.stdout to the print file parameter. Presumably so that a different file handler can be supplied if desired.

Is the correct method in my tests to try to mock the libraries use of sys.stdout instead of capsys then?

@RonnyPfannschmidt
Copy link
Member

if the library does not ensure its taking the current stdout as a default, then it is broken in terms of stdout redirection and that "breakage" has to be worked around

a potential way would be to wrap the library with a redirect_stdout contextmanager which replaces the default stdout object with one that lazily uses sys.stdout so that the library is tricked

how feasible that is depends on the details of the library

it should be noted, that print(..., file=None) works correct, perhaps its time to fix the defaults

@shepherdjay
Copy link
Author

Thanks for the help. I will do some research on how to get the current sys.stdout and work with the library maintainer on a pull request. That sounds like a better long term solution.

In the meantime I’ll also adjust my code accordingly.

@RonnyPfannschmidt
Copy link
Member

btw, whats the library in question?

@shepherdjay
Copy link
Author

NagiosPlugin

https://github.com/mpounsett/nagiosplugin

I had already raised an issue on that repo as well but wanted to check both avenues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: capture related to the capture builtin plugin type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

2 participants