Skip to content

Testing

inline_snapshot.testing provides tools which can be used to test inline-snapshot workflows. This might be useful if you want to build your own libraries based on inline-snapshot.

The following example shows how you can use the Example class to test what inline-snapshot would do with given the source code. The snapshots in the argument are asserted inside the run_* methods, but only when they are provided.

from inline_snapshot.testing import Example
from inline_snapshot import snapshot


def test_something():

    Example(
        {
            "test_a.py": """\
from inline_snapshot import snapshot
def test_a():
    assert 1+1 == snapshot()
"""
        }
    ).run_inline(  # run without flags
        reported_categories=snapshot(),
    ).run_pytest(  # run without flags and check the pytest report
        changed_files=snapshot(),
        report=snapshot(),
    ).run_pytest(  # run with create flag and check the changed files
        ["--inline-snapshot=create"],
        changed_files=snapshot(),
    )

from inline_snapshot.testing import Example
from inline_snapshot import snapshot


def test_something():

    Example(
        {
            "test_a.py": """\
from inline_snapshot import snapshot
def test_a():
    assert 1+1 == snapshot()
"""
        }
    ).run_inline(  # run without flags
        reported_categories=snapshot(["create"]),
    ).run_pytest(  # run without flags and check the pytest report
        changed_files=snapshot({}),
        report=snapshot(
            """\
Error: one snapshot is missing a value (--inline-snapshot=create)
You can also use --inline-snapshot=review to approve the changes interactiv\
"""
        ),
    ).run_pytest(  # run with create flag and check the changed files
        ["--inline-snapshot=create"],
        changed_files=snapshot(
            {
                "test_a.py": """\
from inline_snapshot import snapshot
def test_a():
    assert 1+1 == snapshot(2)
"""
            }
        ),
    )

API

Example

__init__(files)

Parameters:

Name Type Description Default
files str | dict[str, str]

a collecton of files where inline-snapshot opperates on, or just a string which will be saved as test_something.py.

required

run_inline(args=[], *, reported_categories=None, changed_files=None, raises=None)

Execute the example files in process and run every test_* function.

This is useful for fast test execution.

Parameters:

Name Type Description Default
args list[str]

inline-snapshot arguments (supports only "--inline-snapshot=fix|create|..." ).

[]
reported_categories Snapshot[list[Category]] | None

snapshot of categories which inline-snapshot thinks could be applied.

None
changed_files Snapshot[dict[str, str]] | None

snapshot of files which are changed by this run.

None
raises Snapshot[str] | None

snapshot of the exception which is raised during the test execution. It is required if your code raises an exception.

None

Returns:

Type Description
Example

A new Example instance which contains the changed files.

run_pytest(args=[], *, env={}, changed_files=None, report=None, returncode=None)

Run pytest with the given args and env variables in an seperate process.

It can be used to test the interaction between your code and pytest, but it is a bit slower than run_inline

Parameters:

Name Type Description Default
args list[str]

pytest arguments like "--inline-snapshot=fix"

[]
env dict[str, str]

dict of environment variables

{}
changed_files Snapshot[dict[str, str]] | None

snapshot of files which are changed by this run.

None
report Snapshot[str] | None

snapshot of the report at the end of the pytest run.

None
returncode Snapshot[int] | None

snapshot of the pytest returncode.

None

Returns:

Type Description
Example

A new Example instance which contains the changed files.