diff --git a/docs/tutorial/testing.md b/docs/tutorial/testing.md index cbabfd5d04..1628a5de87 100644 --- a/docs/tutorial/testing.md +++ b/docs/tutorial/testing.md @@ -81,6 +81,36 @@ If you need a refresher about what is "standard output" and "standard error" che /// +#### Checking rich output + +Rich attempts to adjust the formatting to the connected terminal's size, wrapping or shortening content to make it fit. +However, in many test environments, there is no terminal attached to the process, and therefore Rich will fallback to a standard terminal width of 80 characters. + +In some cases this might not be sufficient to test the output of a command. +It is possible to override the width of a Rich `Console` object using the environment variable `COLUMNS`. +This applies to all instances of a console created in the application. + +/// tip + +To avoid polluting the environment outside of the current test run, use monkeypatch to set the environment variable: + +```Python +@pytest.fixture() +def console_width(monkeypatch): + monkeypatch.setenv("COLUMNS", "200") +``` + +/// + +/// tip + +Typer also creates such an instance, which is used for output produced by Typer, such as the help display. +If you only want to modify the output width of this internal usage, you can set the environment variable `TERMINAL_WIDTH`. + +It is necessary to set `TERMINAL_WIDTH` *before* the Typer app is imported. + +/// + ### Call `pytest` Then you can call `pytest` in your directory and it will run your tests: