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

[Suggestion] Use a namedtuple for capsys results #2879

Closed
samueldg opened this Issue Oct 27, 2017 · 5 comments

Comments

Projects
None yet
3 participants
@samueldg
Contributor

samueldg commented Oct 27, 2017

Currently, the capsys fixture's readouterr() method returns a tuple with (stdout, stderr) results.

Usage will look something like this:

def test_print_1(capsys):
    print('something')
    out, err = capsys.readouterr()  # Named, unused variable
    assert 'something' in out

def test_print_2(capsys):
    print('something ')
    out, _ = capsys.readouterr()  # Anonymous, ignored variable
    assert 'something' in out

def test_print_3(capsys):
    print('something')
    assert 'something' in capsys.readouterr()[0]  # Index access (not super readable)

My suggestion would be to return a namedtuple instead, so the attributes can be accessed by name

def test_print_4(capsys):
    print('something')
    assert 'something' in capsys.readouterr().out  # Attribute access

Which could be achieved by wrapping the result with something like:

from collections import namedtuple
CapsysResult = namedtuple('CapsysResult', ['out', 'err'])

This would be pretty straightforward, and still fully compatible with existing code using tuples either for index access or unpacking.

@samueldg

This comment has been minimized.

Contributor

samueldg commented Oct 27, 2017

Let me know if that's something you'd want in, I'd gladly submit a PR.

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Oct 27, 2017

@samueldg that's a perfect case where changing to a namedtuple is a great idea. A PR would be greatly appreciated, thanks! (Make sure to target the features branch)

samueldg added a commit to samueldg/pytest that referenced this issue Oct 28, 2017

@samueldg

This comment has been minimized.

Contributor

samueldg commented Oct 28, 2017

@nicoddemus There you go: #2880

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Oct 29, 2017

Awesome, thanks!

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Oct 30, 2017

closed by #2880

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment