[Suggestion] Use a namedtuple for capsys results
See original GitHub issueCurrently, 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:
- Created 6 years ago
- Comments:5 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@nicoddemus There you go: https://github.com/pytest-dev/pytest/pull/2880
closed by #2880