Skip to main content

Snapshot testing for Textual apps

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_textual_snapshot-1.0.0.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

pytest_textual_snapshot-1.0.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest_textual_snapshot-1.0.0.tar.gz.

File metadata

  • Download URL: pytest_textual_snapshot-1.0.0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.5.0

File hashes

Hashes for pytest_textual_snapshot-1.0.0.tar.gz
Algorithm Hash digest
SHA256 065217055ed833b8a16f2320a0613f39a0154e8d9fee63535f29f32c6414b9d7
MD5 bb7c5d3259eb51b3a1b7f93ed7689285
BLAKE2b-256 8b752ef17ae52fa5bc848ff2d1d7bc317a702cbd6d7ad733ca991b9f899dbbae

See more details on using hashes here.

File details

Details for the file pytest_textual_snapshot-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_textual_snapshot-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd3a421491a6b1987ee7b4336d7f65299524924d2b0a297e69733b73b01570e1
MD5 34ea8cc4d96580e3a0b64b8395af723a
BLAKE2b-256 702e4bf16ed78b382b3d7c1e545475ec8cf04346870be662815540faf8f16e8c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page