Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] How to test output of rich.Table? #247

Closed
Imipenem opened this issue Aug 22, 2020 · 8 comments
Closed

[QUESTION] How to test output of rich.Table? #247

Imipenem opened this issue Aug 22, 2020 · 8 comments

Comments

@Imipenem
Copy link

Hey, first thanks for this awesome package.

I have a small question on how to test for output of a rich Table, assume I have the following code:

table = Table(title=f'[bold]MyTitle of {myproject}', ="blue", header_style=Style(color="blue", bold=True), box=HEAVY_HEAD)

        table.add_column("Column1", justify="left", style="green", no_wrap=True)
        table.add_column("Column2", justify="left")
        table.add_column("Column3", justify="left")

        for item in mylist:
            table.add_row(f'[bold]{template[0]}', template[1], template[2], template[3], template[4])

        console = Console()
        console.print(table)

Now while testing this (with different values for myproject) using pytest I'd like to check if this table meets some requirements (so I'd like to check for example if the title is right).

How would one do this?
My naive approach was just to capture the output with pytest (basically a big string) a test whether it contains my desired title. However this does not work, as the output is something like:

MyTitle of title1                        \n┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━... PyPi.     │           │         │\n└────────────────────────────┴────────────┴──────────────┴───────────┴─────────┘\n

So TL;DR: Is there any nice way to test the output of a table? Thanks in advance ;)

@willmcgugan
Copy link
Collaborator

The easiest way to capture output from rich is to set the file to a StringIO. Something like this:

console = Console(file=io.StringIO(), width=120)
console.print(table)
output = console.file.getvalue()

You could also test the attributes of the Table object directly. e.g. assert table.title == "MyTitle".

@Imipenem
Copy link
Author

I see that looks good, thanks for the quick reply.

@gmankab
Copy link

gmankab commented Apr 10, 2022

another way - https://rich.readthedocs.io/en/stable/reference/console.html#rich.console.Console.export_text

console = rich.console.Console(
    record = True
)

console.print(
    '[red]hello world!'
)

text = console.export_text()

it will be printed in console and saved in variable

@timothygebhard
Copy link

To add to @gmankab's answer: If you just want your rendered table in string without writing it to the terminal, you might want to have a look at capturing:

from rich.console import Console
console = Console()
with console.capture() as capture:
    console.print(table)
table_str = capture.get()

@gmankab
Copy link

gmankab commented Jan 5, 2023

@timothygebhard thanks

@noisy
Copy link

noisy commented Mar 27, 2024

The easiest way to capture output from rich is to set the file to a StringIO. Something like this:

console = Console(file=io.StringIO(), width=120)
console.print(table)
output = console.file.getvalue()

You could also test the attributes of the Table object directly. e.g. assert table.title == "MyTitle".

@willmcgugan that's works. But how to print to file a table with a color info?

@willmcgugan
Copy link
Collaborator

https://rich.readthedocs.io/en/latest/console.html#capturing-output

@noisy
Copy link

noisy commented Mar 27, 2024

@willmcgugan Ok. Thanks. At the end I was able to do this, so I called it a success :)

SCR-20240327-lpoe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants