After a bit of testing, I’ve managed to get a reproducible code snippet:
from pytest import CaptureFixture
from rich.table import Table
from rich import print, box
from datetime import datetime
expected = """
| Released | Title | Box Office | Genre | Director |
|----------------------------|----------------------------------|--------------|-----------------|-------------|
| 9999-12-31T23:59:59.999999 | Star Wars: The Rise of Skywalker | $952,110,690 | Sci-Fi, Fantasy | J.J. Abrams |
"""
def test_stdout(capsys: CaptureFixture[str]) -> None:
table = Table(title="", box=box.MARKDOWN)
table.add_column("Released", no_wrap=True)
table.add_column("Title", no_wrap=True)
table.add_column("Box Office", no_wrap=True)
table.add_column("Genre", no_wrap=True)
table.add_column("Director", no_wrap=True)
table.add_row(
datetime.max.isoformat(),
"Star Wars: The Rise of Skywalker",
"$952,110,690",
"Sci-Fi, Fantasy",
"J.J. Abrams",
)
print(table)
# print(table, file=open("stdout.txt", "w"))
assert capsys.readouterr().out.strip() == expected.strip()
# open("capsys.txt", "w").write(capsys.readouterr().out)
Error
❯ pytest -vvvv
=================================================================================== test session starts ===================================================================================
platform win32 -- Python 3.12.3, pytest-8.2.0, pluggy-1.5.0 -- C:\Users\monarch\Documents\GitHub\abcdef\.venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\monarch\Documents\GitHub\abcdef
configfile: pyproject.toml
collected 1 item
tests/test_stdout.py::test_stdout FAILED [100%]
======================================================================================== FAILURES =========================================================================================
_______________________________________________________________________________________ test_stdout _______________________________________________________________________________________
capsys = <_pytest.capture.CaptureFixture object at 0x00000266B3386690>
def test_stdout(capsys: CaptureFixture[str]) -> None:
table = Table(title="", box=box.MARKDOWN)
table.add_column("Released", no_wrap=True)
table.add_column("Title", no_wrap=True)
table.add_column("Box Office", no_wrap=True)
table.add_column("Genre", no_wrap=True)
table.add_column("Director", no_wrap=True)
table.add_row(
datetime.max.isoformat(),
"Star Wars: The Rise of Skywalker",
"$952,110,690",
"Sci-Fi, Fantasy",
"J.J. Abrams",
)
print(table)
# print(table, file=open("stdout.txt", "w"))
> assert capsys.readouterr().out.strip() == expected.strip()
E AssertionError: assert '| Released | Title | Box … | Genre | Dir… |\n|---------------------|----------------------------|-------|-----------|------|\n| 9999-12-31T23:59:5… | Star Wars: The Rise of Sk… | $952… | Sci-Fi, … | J.J… |' == '| Released | Title | Box Office | Genre
| Director |\n|----------------------------|----------------------------------|--------------|-----------------|-------------|\n| 9999-12-31T23:59:59.999999 | Star Wars: The Rise of Skywalker | $952,110,690 | Sci-Fi, Fantasy | J.J. Abrams |'
E
E - | Released | Title | Box Office | Genre | Director |
E ? ------- ------ ^^^^^^^^ ------ ^^^^^^^^
E + | Released | Title | Box … | Genre | Dir… |
E ? ^ ^
E - |----------------------------|----------------------------------|--------------|-----------------|-------------|
E ? ------- ------ ------- ------ -------
E + |---------------------|----------------------------|-------|-----------|------|
E - | 9999-12-31T23:59:59.999999 | Star Wars: The Rise of Skywalker | $952,110,690 | Sci-Fi, Fantasy | J.J. Abrams |
E ? ^^^^^^^^ ^^^^^^^ ^^^^^^^^ ^^^^^^^ ^^^^^^^^
E + | 9999-12-31T23:59:5… | Star Wars: The Rise of Sk… | $952… | Sci-Fi, … | J.J… |
E ? ^ ^ ^ ^ ^
tests\test_stdout.py:31: AssertionError
================================================================================= short test summary info =================================================================================
FAILED tests/test_stdout.py::test_stdout - AssertionError: assert '| Released | Title | Box … | Genre | Dir… |\n|---------------------|----------------------------|-------|-----------|------|\n| 9999-12-31T23:59:5… | Star Wars: The Rise of Sk… | $952… | Sci-Fi, … | J.J… |' == '| Released | Title
| Box Office | Genre | Director |\n|----------------------------|----------------------------------|--------------|-----------------|-------------|\n| 9999-12-31T23:59:59.999999 | Star Wars: The Rise of Skywalker | $952,110,690 | Sci-Fi, Fantasy | J.J. Abrams |'
- | Released | Title | Box Office | Genre | Director |
? ------- ------ ^^^^^^^^ ------ ^^^^^^^^
+ | Released | Title | Box … | Genre | Dir… |
? ^ ^
- |----------------------------|----------------------------------|--------------|-----------------|-------------|
? ------- ------ ------- ------ -------
+ |---------------------|----------------------------|-------|-----------|------|
- | 9999-12-31T23:59:59.999999 | Star Wars: The Rise of Skywalker | $952,110,690 | Sci-Fi, Fantasy | J.J. Abrams |
? ^^^^^^^^ ^^^^^^^ ^^^^^^^^ ^^^^^^^ ^^^^^^^^
+ | 9999-12-31T23:59:5… | Star Wars: The Rise of Sk… | $952… | Sci-Fi, … | J.J… |
? ^ ^ ^ ^ ^
==================================================================================== 1 failed in 0.18s ====================================================================================
What actually gets captured by capsys looks like this:
| Released | Title | Box ďż˝ | Genre | Dirďż˝ |
|---------------------|----------------------------|-------|-----------|------|
| 9999-12-31T23:59:5ďż˝ | Star Wars: The Rise of Skďż˝ | $952ďż˝ | Sci-Fi, ďż˝ | J.Jďż˝ |