Skip to main content

pytest-textual-snapshot

A pytest plugin for snapshot testing Textual applications.

image

Installation

Install using pip:

pip install pytest-textual-snapshot

After installing, the snap_compare fixture will automatically be made available.

About

A pytest-textual-snapshot test saves an SVG screenshot of a running Textual app to disk. The next time the test runs, it takes another screenshot and compares it to the saved one. If the new screenshot differs from the old one, the test fails. This is a convenient way to quickly and automatically detect visual regressions in your applications.

Usage

Running tests

You can run your tests using pytest as normal. You can use pytest-xdist to run your tests in parallel.

My snapshot test failed, what do I do?

If your snapshot test fails, it means that the screenshot taken during the test session differs from the last screenshot taken. This change is shown in the failure report, which you'll be given a linked to in the event of a failure.

If the diff shown in the failure report looks correct, you can update the snapshot on disk by running pytest with the --snapshot-update flag.

Writing tests

Basic usage

Inject the snap_compare fixture into your test and call it with an app instance or the path to the Textual app (the file containing the App subclass).

def test_my_app(snap_compare):
    app = MyTextualApp()  # a *non-running* Textual `App` instance
    assert snap_compare(app)
def test_something(snap_compare):
    assert snap_compare("path/to/app.py")

Pressing keys

Key presses can be simulated before the screenshot is taken.

def test_something(snap_compare):
    assert snap_compare("path/to/app.py", press=["tab", "left", "a"])

Run code before screenshot

You can run some code before capturing a screenshot using the run_before parameter.

def test_something(snap_compare):
    async def run_before(pilot: Pilot):
        await pilot.press("ctrl+p")
        # You can run arbitrary code before the screenshot occurs:
        await disable_blink_for_active_cursors(pilot)
        await pilot.press(*"view")

    assert snap_compare(MyApp(), run_before=run_before)

Customizing the size of the terminal

If you need to change the size of the terminal (for example to fit in more content or test layout-related code), you can adjust the terminal_size parameter.

def test_another_thing(snap_compare):
    assert snap_compare(MyApp(), terminal_size=(80, 34))

Quickly opening paths in your editor

If you passed a path to snap_compare, you can quickly open the path in your editor by setting the TEXTUAL_SNAPSHOT_FILE_OPEN_PREFIX environment variable based on the editor you want to use. Clicking on the path in the snapshot report will open the path in your editor.

  • file:// - default, most likely opening in your browser
  • code://file/ - opens the path in VS Code
  • cursor://file/ - opens the path in Cursor
  • pycharm:// - opens the path in PyCharm

Release files for pytest-textual-snapshot 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-textual-snapshot 1.1.0
File Size Uploaded
pytest_textual_snapshot-1.1.0.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-textual-snapshot 1.1.0
File Interpreter ABI Platform
pytest_textual_snapshot-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.8 kB

Release files / pytest_textual_snapshot-1.1.0.tar.gz

Download URL pytest_textual_snapshot-1.1.0.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
96d48ab01306852a3b4ae165f008d5fdd7fda777e91e9d2c3ea0f7d7458544eb
BLAKE2b-256 checksum
How to use checksums
3e7f4135f87e12c1c46376971fec5ebfe71f7f8b15ac20f887c90932dedd6e78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.7 Darwin/24.2.0

Release files / pytest_textual_snapshot-1.1.0-py3-none-any.whl

Download URL pytest_textual_snapshot-1.1.0-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fdf7727d2bc444f947554308da1b08df7a45215fe49d0621cbbc24c33e8f7b8d
BLAKE2b-256 checksum
How to use checksums
1c30c31d800f8d40d663fc84d83548b26aecf613c9c39bd6985c813d623d7b84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.7 Darwin/24.2.0

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page