pytest

24 posts tagged “pytest”.

  1. 2025
  2. Checking if something is callable or async callable in Python

    I wanted a mechanism to check if a given Python object was "callable" - could be called like a function - or "async callable" - could be called using .

    1 min read
  3. Snapshot testing with Syrupy

    I'm a big fan of snapshot testing - writing tests where you compare the output of some function to a previously saved version, and can re-generate that version from scratch any time something changes.

    2 min read
  4. Asserting a dictionary is a subset of another dictionary

    Take for example an API endpoint that returns something like this: I want to efficiently assert against the second two keys, but I don't want to hard-code the SQLite version into my test in case it…

    1 min read
  5. Testing a Click app with streaming input

    For sqlite-utils 364 I needed to write a test for a Click app which dealt with input streamed to standard input.

    1 min read
  6. Mocking a Textract LimitExceededException with boto

    For s3-ocr issue 21 I needed to write a test that simulates what happens when Amazon Textract returns a "LimitExceededException".

    1 min read
  7. How to mock httpx using pytest-mock

    I wrote this test to exercise some httpx code today, using pytest-mock. The key was to use which patches the module that was imported by the module.

    1 min read
  8. Using VCR and pytest with pytest-recording

    pytest-recording is a neat pytest plugin that makes it easy to use the VCR library, which helps write tests against HTTP resources by automatically capturing responses and baking them into a YAML file…

    1 min read
  9. Session-scoped temporary directories in pytest

    I habitually use the fixture in pytest to get a temporary directory that will be cleaned up after each test, but that doesn't work with - which can be used to ensure an expensive fixture is run only…

    1 min read
  10. Async fixtures with pytest-asyncio

    I wanted to use a fixture with that was itsef as function, so that it could execute statements. Since I'm using a file containing I had to use the fixture to get this to work.

    1 min read
  11. 2024
  12. Treating warnings as errors in pytest

    I was seeing this warning in a Django project when I thought I was correctly using timezone-aware dates everywhere: RuntimeWarning: DateTimeField Shift.shift start received a naive datetime…

    1 min read
  13. Mocking Stripe signature checks in a pytest fixture

    I'm writing some code that accepts webhooks from Stripe. I wanted to simulate hits to this endpoint in my Django tests.

    1 min read
  14. Running pytest against a specific Python version with uv run

    While working on this issue I figured out a neat pattern for running the tests for my project locally against a specific Python version using uv run: The new-to-me trick here is that works for adding…

    1 min read
  15. pytest coverage with context

    This tweet from \@Mariatta tipped me off to the ability to measure "contexts" when running coverage - as a way to tell which tests exercise which specific lines of code.

    1 min read
  16. Start a server in a subprocess during a pytest session

    I wanted to start an actual server process, run it for the duration of my pytest session and shut it down at the end. Here's the recipe I came up with.

    1 min read
  17. Registering temporary pluggy plugins inside tests

    While implementing more finely-grained permissions for (issue 8) I decided I wanted to register a Datasette pluggy plugin for the duration of a single test.

    1 min read
  18. Mocking subprocess with pytest-subprocess

    For apple-notes-to-sqlite I needed to write some tests that simulated executing the command using the Python module. I wanted my tests to run on Linux CI machines, where that command would not exist.

    1 min read
  19. Show files opened by pytest tests

    My test suite for Datasette has grown so large that running the whole thing sometimes causes me to run out of file handles.

    1 min read
  20. 2023
  21. Using namedtuple for pytest parameterized tests

    I'm writing some quite complex pytest parameterized tests this morning, and I was finding it a little bit hard to read the test cases as the number of parameters grew.

    1 min read
  22. Quick and dirty mock testing with mock_calls

    I needed to write a test that checked for a really complex sequence of mock calls for s3-credentials 3.

    1 min read
  23. Writing pytest tests against tools written with argparse

    I usually build command-line tools using Click (and my click-app cookiecutter template), which includes a really nice set of tools for writing tests.

    1 min read