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

capsys does not follow the OS newline rules for print #3207

Closed
gaborbernat opened this issue Feb 12, 2018 · 1 comment
Closed

capsys does not follow the OS newline rules for print #3207

gaborbernat opened this issue Feb 12, 2018 · 1 comment
Labels
platform: windows windows platform-specific problem plugin: capture related to the capture builtin plugin

Comments

@gaborbernat
Copy link
Contributor

gaborbernat commented Feb 12, 2018

Given the following code (test_a.py):

def main():
    print()
    print()

import os
def test(capsys):
    main()
    out, err = capsys.readouterr()
    expected = os.linesep.join(['', '', ''])
    assert out == expected
λ python -m pytest
============================= test session starts =============================
platform win32 -- Python 3.6.1, pytest-3.4.0, py-1.5.2, pluggy-0.6.0
rootdir: D:\work\test, inifile:
plugins: devpi-server-4.4.0
collected 1 item

test_main.py F                                                           [100%]

================================== FAILURES ===================================
____________________________________ test _____________________________________

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

    def test(capsys):
        main()
        out, err = capsys.readouterr()
        expected = os.linesep.join(['', '', ''])
>       assert out == expected
E       AssertionError: assert '\n\n' == '\r\n\r\n'
test_main.py:11: AssertionError
========================== 1 failed in 0.13 seconds ===========================
λ python -c "import sys; print(sys.platform)"
win32

λ python -c "import sys; print(sys.version)"
3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)]

Surely capsys should use the os.linesep exactly as that's the case with the print function:

python -c "import subprocess; import sys; print(repr(subprocess.check_output([sys.executable, '-c', 'print(); print()'])))"
b'\r\n\r\n'
@pytestbot pytestbot added the plugin: capture related to the capture builtin plugin label Feb 12, 2018
@nicoddemus nicoddemus added type: bug problem that needs to be addressed platform: windows windows platform-specific problem labels Feb 22, 2018
@aklajnert aklajnert removed the type: bug problem that needs to be addressed label Feb 27, 2020
@aklajnert
Copy link
Contributor

aklajnert commented Feb 27, 2020

That's not a bug - that's the default behavior of the print() function. As you can see in the documentation, the print() function has default argument end='\n', which is responsible for the end of line. You can change your print() to print(end=os.linesep) to make your test pass.

The subprocess.check_output() output has system-specific line ends which will be captured by capsys correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: windows windows platform-specific problem plugin: capture related to the capture builtin plugin
Projects
None yet
Development

No branches or pull requests

4 participants