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

[Suggestion] Use a namedtuple for capsys results #2879

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

[Suggestion] Use a namedtuple for capsys results #2879

samueldg opened this issue Oct 27, 2017 · 5 comments

Comments

@samueldg
Copy link
Contributor

@samueldg 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
Copy link
Contributor Author

@samueldg samueldg commented Oct 27, 2017

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

@nicoddemus
Copy link
Member

@nicoddemus 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
Copy link
Contributor Author

@samueldg samueldg commented Oct 28, 2017

@nicoddemus There you go: #2880

@nicoddemus
Copy link
Member

@nicoddemus nicoddemus commented Oct 29, 2017

Awesome, thanks!

@RonnyPfannschmidt
Copy link
Member

@RonnyPfannschmidt 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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants