DEV Community

Cover image for Snapshot Testing in Python with pytest-verify — Part 2: Async Support
Mohamed Tahri
Mohamed Tahri

Posted on

Snapshot Testing in Python with pytest-verify — Part 2: Async Support

In the previous article, we explored how pytest-verify makes snapshot testing effortless for structured data — JSON, YAML, XML, DataFrames, and more.

But what if your tests are asynchronous?
Modern Python apps rely heavily on async I/O — think of:

  • Async web frameworks like FastAPI, aiohttp, or Quart

  • Async database drivers (asyncpg, motor)

  • Message brokers and streaming systems

Until now, testing async functions meant juggling plugins and decorators.
Not anymore 🚀

Introducing Native Async Support

Starting from pytest-verify v1.2.0, you can snapshot async test functions directly — no wrappers, no extra ceremony.

Whether your test is async or simply returns a coroutine, @verify_snapshot now handles both seamlessly:

⚙️ Setup

1.Install the async extras:

pip install pytest-verify[async]
Enter fullscreen mode Exit fullscreen mode

2.Enable async mode in your pytest configuration (either in pytest.ini or pyproject.toml):

pyproject.toml:

[tool.pytest.ini_options]
asyncio_mode = "auto"
Enter fullscreen mode Exit fullscreen mode

pytest.ini:

[pytest]
asyncio_mode = auto
Enter fullscreen mode Exit fullscreen mode

Example 1 — Async REST API Snapshot

Let’s simulate a lightweight async API call:

import asyncio
from pytest_verify import verify_snapshot

async def get_user():
    await asyncio.sleep(0.1)
    return {"id": 123, "name": "Mohamed", "country": "France"}

@verify_snapshot()
async def test_async_user_snapshot():
    """Ensure async API output stays stable."""
    user = await get_user()
    return user
Enter fullscreen mode Exit fullscreen mode

✅ On first run → baseline snapshot created.
✅ On next runs → automatic comparison with full diff view if anything changes.

Example 2 — Async Data Transformation

You can also snapshot results from async pipelines or background tasks:

from pytest_verify import verify_snapshot


async def compute_metrics():
        return {"accuracy": 99.94, "loss": 0.102}


@verify_snapshot(abs_tol=0.1)
async def test_async_data_pipeline():
    """Verify numeric tolerance in async output."""
    return await compute_metrics()
Enter fullscreen mode Exit fullscreen mode

✅ pytest-verify waits for the coroutine, serializes the returned dict, and applies your configured tolerances automatically — no extra setup.

Example 3 — Async Ignore Fields and Tolerances

You can combine async support with all snapshot options:

from pytest_verify import verify_snapshot
import asyncio, random

@verify_snapshot(
    ignore_fields=["$.meta.timestamp"],
    abs_tol_fields={"$.metrics.latency": 1.0}
)
async def test_async_snapshot_with_ignores():
    await asyncio.sleep(0.05)
    return {
        "meta": {"timestamp": "2025-10-26T09:00:00Z"},
        "metrics": {"latency": random.uniform(99, 101)},
    }
Enter fullscreen mode Exit fullscreen mode

✅ This test ignores the volatile timestamp field
✅ Allows ±1.0 drift for latency values
✅ Works perfectly under async execution

Wrapping Up — Async Testing Made Effortless

In modern Python, async is everywhere — your tests shouldn’t lag behind.
With pytest-verify >= v1.2.0, you can now:

  • Snapshot async APIs and coroutine results directly.

  • Use all features — ignore fields, tolerances, diff viewer — in async mode.

  • Keep your test suite consistent and declarative.

  • No extra plugins. No decorator juggling. Just pure, powerful snapshot testing.

💡 Final Thought

If your tests look like this 👇:

await some_async_func()
assert ...
assert ...
assert ...
assert ...
Enter fullscreen mode Exit fullscreen mode

You can now replace them with:

@verify_snapshot()
async def test_async_output():
    return await some_async_func()
Enter fullscreen mode Exit fullscreen mode

Cleaner, safer, and instantly snapshot-aware.

If you find this new async support useful, give pytest-verify a ⭐ on GitHub and
share your feedback!

Top comments (0)