question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Suggestion] Use a namedtuple for capsys results

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
samueldgcommented, Oct 28, 2017
0reactions
RonnyPfannschmidtcommented, Oct 30, 2017

closed by #2880

Read more comments on GitHub >

github_iconTop Results From Across the Web

Write Pythonic and Clean Code With namedtuple - Real Python
It allows you to create tuple subclasses with named fields. You can access the values in a given named tuple using the dot...
Read more >
pytest capsys: checking output AND getting it reported?
You're seeing the correct behaviour, when using capsys.readouterr() you're consuming the captured output. Hence any output to stdout and ...
Read more >
Historical errata for Python Testing with pytest
PDF Pg Type Fixed on 67 ERROR 2017‑06‑07 12 TYPO 2017‑06‑07 2 TYPO 2017‑06‑07
Read more >
pytest Documentation - Read the Docs
a result, the two test functions using smtp_connection run as quick as a ... Also pytest doesn't follow PEP 506 suggestion of resetting...
Read more >
Understand how to use NamedTuple and Dataclass in Python
We can avoid these problems using Named Tuple. Named Tuple allows us ... Assign field names to result in tuples returned by csv...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found