[BUG] Rich is incompatible with pytest capsys fixture
See original GitHub issueDescribe the bug Adopters of rich will eventually discover that their use of capsys in pytest unittest gets broken when they integrate the library.
While this is somehow expected we should document a recipe for adapting testing in such way that does not break when using rich.
Example test:
import pytest
def test_output(capsys):
foo() # expected to produce stdout content
stdout, stderr = capsys.readouterr()
assert stdout == "..."
assert stderr == "..."
This has repercussions even for logging functions and I even encountered errors when parametrizing tests like ValueError: I/O operation on closed file.
which are caused by Console() instance being closed after the first test.
As rich itself makes use of pytest it should not be a big deal to include an example.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
[BUG] Rich is incompatible with pytest capsys fixture #317
Describe the bug. Adopters of rich will eventually discover that their use of capsys in pytest unittest gets broken when they integrate the ......
Read more >API Reference — pytest documentation
Fixtures are requested by test functions or other fixtures by declaring them as argument names. Example of a test requiring a fixture: def...
Read more >stdout not captured by pytest capsys when using fixture
Here, capsys does not capture sys.stderr properly. If I move from sys import stdout and args.out = stdout directly into the test function, ......
Read more >pytest Documentation - Read the Docs
pytest provides Builtin fixtures/function arguments to request arbitrary resources, like a unique temporary directory:.
Read more >Reference — pytest documentation
@pytest.fixture; config.cache; capsys; capsysbinary; capfd; capfdbinary ... This is because the call hierarchy of rich comparisons follows a fixed behavior.
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 FreeTop 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
Top GitHub Comments
I’m having the same issue, I used
click.echo
and it worked, but now I tried usingrich
and when I’m looking at the:capsys.readouterr()
I’m seeing partial log was captured, that is some of the output is missing. Any ideas what’s causing this? (I’m using rich version11.1.0
, with python=3.7.12).I am experiencing this problem only with rich Tree output. Rich tables and other output is testable in
capsys
, but the tree doesn’t appear there. I can see the tree happening, it just isn’t captured.Edit: I have tried using the example above to custom echo the tree. I have always tried the
rich-click
plugin. Edit 2: Maybe this is a different problem because even with-s
, the tree doesn’t appear Edit 3: Found one solution: send the output to a separate file and then read the file and make assertions there. Also note you have to close the file first for this to workEDIT 4: By explicitly setting the
file
tosys.stdout
, everything worked 😃Edit 5: Okay, clarification: I was setting the file to
sys.stdout
in the lib that made the call. HOWEVER, it only works when setting thefile
tosys.stdout
in the actual test… This seems like a bug!Edit 6: Ok, resorting back to edit 3 because when using
sys.stdout
, most of the output is lost wherever there is markup characters, so it is not accurate. However when using a separate file, I can see all the output.