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.

[BUG] Rich is incompatible with pytest capsys fixture

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kat25230commented, Feb 2, 2022

I’m having the same issue, I used click.echo and it worked, but now I tried using rich 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 version 11.1.0, with python=3.7.12).

0reactions
unparalleled-jscommented, Sep 20, 2022

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 work

EDIT 4: By explicitly setting the file to sys.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 the file to sys.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.

Read more comments on GitHub >

github_iconTop 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 >

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