Skip to content

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

Closed
@Imipenem

Description

@Imipenem

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 ;)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions