pytest-textual-snapshot


Namepytest-textual-snapshot JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/darrenburns/pytest-textual-snapshot
SummarySnapshot testing for Textual apps
upload_time2024-07-22 15:17:44
maintainerDarren Burns
docs_urlNone
authorDarren Burns
requires_python<4.0.0,>=3.8.1
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # `pytest-textual-snapshot`

A pytest plugin for snapshot testing Textual applications.

<img width="1325" alt="image" src="https://github.com/user-attachments/assets/a5079356-ef0f-4c7e-9ed2-bf2115e75c4f">

## 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).

```python
def test_my_app(snap_compare):
    app = MyTextualApp()  # a *non-running* Textual `App` instance
    assert snap_compare(app)
```

```python
def test_something(snap_compare):
    assert snap_compare("path/to/app.py")
``` 

#### Pressing keys

Key presses can be simulated before the screenshot is taken.

```python
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.

```python
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.

```python
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


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/darrenburns/pytest-textual-snapshot",
    "name": "pytest-textual-snapshot",
    "maintainer": "Darren Burns",
    "docs_url": null,
    "requires_python": "<4.0.0,>=3.8.1",
    "maintainer_email": "darren@textualize.io",
    "keywords": null,
    "author": "Darren Burns",
    "author_email": "darren@textualize.io",
    "download_url": "https://files.pythonhosted.org/packages/8b/75/2ef17ae52fa5bc848ff2d1d7bc317a702cbd6d7ad733ca991b9f899dbbae/pytest_textual_snapshot-1.0.0.tar.gz",
    "platform": null,
    "description": "# `pytest-textual-snapshot`\n\nA pytest plugin for snapshot testing Textual applications.\n\n<img width=\"1325\" alt=\"image\" src=\"https://github.com/user-attachments/assets/a5079356-ef0f-4c7e-9ed2-bf2115e75c4f\">\n\n## Installation\n\nInstall using `pip`:\n\n```\npip install pytest-textual-snapshot\n```\n\nAfter installing, the `snap_compare` fixture will automatically be made available.\n\n## About\n\nA `pytest-textual-snapshot` test saves an SVG screenshot of a running Textual app to disk. \nThe next time the test runs, it takes another screenshot and compares it to the saved one.\nIf the new screenshot differs from the old one, the test fails.\nThis is a convenient way to quickly and automatically detect visual regressions in your applications.\n\n## Usage\n\n### Running tests\n\nYou can run your tests using `pytest` as normal. You can use `pytest-xdist` to run your tests in parallel.\n\n#### My snapshot test failed, what do I do?\n\nIf your snapshot test fails, it means that the screenshot taken during the test session\ndiffers from the last screenshot taken.\nThis change is shown in the failure report, which you'll be given a linked to in the event of a failure.\n\nIf the diff shown in the failure report looks correct, you can update the snapshot on disk\nby running `pytest` with the `--snapshot-update` flag.\n\n### Writing tests\n\n#### Basic usage\n\nInject the `snap_compare` fixture into your test and call\nit with an app instance or the path to the Textual app (the file containing the `App` subclass).\n\n```python\ndef test_my_app(snap_compare):\n    app = MyTextualApp()  # a *non-running* Textual `App` instance\n    assert snap_compare(app)\n```\n\n```python\ndef test_something(snap_compare):\n    assert snap_compare(\"path/to/app.py\")\n``` \n\n#### Pressing keys\n\nKey presses can be simulated before the screenshot is taken.\n\n```python\ndef test_something(snap_compare):\n    assert snap_compare(\"path/to/app.py\", press=[\"tab\", \"left\", \"a\"])\n```\n\n#### Run code before screenshot\n\nYou can run some code before capturing a screenshot using the `run_before` parameter.\n\n```python\ndef test_something(snap_compare):\n    async def run_before(pilot: Pilot):\n        await pilot.press(\"ctrl+p\")\n        # You can run arbitrary code before the screenshot occurs:\n        await disable_blink_for_active_cursors(pilot)\n        await pilot.press(*\"view\")\n\n    assert snap_compare(MyApp(), run_before=run_before)\n```\n\n#### Customizing the size of the terminal\n\nIf 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.\n\n```python\ndef test_another_thing(snap_compare):\n    assert snap_compare(MyApp(), terminal_size=(80, 34))\n```\n\n#### Quickly opening paths in your editor\n\nIf 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.\n\n- `file://` - default, most likely opening in your browser\n- `code://file/` - opens the path in VS Code\n- `cursor://file/` - opens the path in Cursor\n- `pycharm://` - opens the path in PyCharm\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Snapshot testing for Textual apps",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/darrenburns/pytest-textual-snapshot"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "702e4bf16ed78b382b3d7c1e545475ec8cf04346870be662815540faf8f16e8c",
                "md5": "34ea8cc4d96580e3a0b64b8395af723a",
                "sha256": "dd3a421491a6b1987ee7b4336d7f65299524924d2b0a297e69733b73b01570e1"
            },
            "downloads": -1,
            "filename": "pytest_textual_snapshot-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "34ea8cc4d96580e3a0b64b8395af723a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0.0,>=3.8.1",
            "size": 11171,
            "upload_time": "2024-07-22T15:17:43",
            "upload_time_iso_8601": "2024-07-22T15:17:43.167080Z",
            "url": "https://files.pythonhosted.org/packages/70/2e/4bf16ed78b382b3d7c1e545475ec8cf04346870be662815540faf8f16e8c/pytest_textual_snapshot-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8b752ef17ae52fa5bc848ff2d1d7bc317a702cbd6d7ad733ca991b9f899dbbae",
                "md5": "bb7c5d3259eb51b3a1b7f93ed7689285",
                "sha256": "065217055ed833b8a16f2320a0613f39a0154e8d9fee63535f29f32c6414b9d7"
            },
            "downloads": -1,
            "filename": "pytest_textual_snapshot-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "bb7c5d3259eb51b3a1b7f93ed7689285",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0.0,>=3.8.1",
            "size": 11071,
            "upload_time": "2024-07-22T15:17:44",
            "upload_time_iso_8601": "2024-07-22T15:17:44.629980Z",
            "url": "https://files.pythonhosted.org/packages/8b/75/2ef17ae52fa5bc848ff2d1d7bc317a702cbd6d7ad733ca991b9f899dbbae/pytest_textual_snapshot-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-22 15:17:44",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "darrenburns",
    "github_project": "pytest-textual-snapshot",
    "github_not_found": true,
    "lcname": "pytest-textual-snapshot"
}
        
Elapsed time: 0.53579s