Simon Willison: TILs on pytest

Atom feed for pytest

pytest Using namedtuple for pytest parameterized tests - 2024-08-31

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. …

pytest Mocking Stripe signature checks in a pytest fixture - 2024-07-01

I'm writing some code that accepts webhooks from Stripe. I wanted to simulate hits to this endpoint in my Django tests. Stripe uses a Stripe-Signature header and I wanted a way to mock my code so that I didn't need to calculate the correct signature. …

pytest Snapshot testing with Syrupy - 2023-09-26

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. …

pytest Mocking subprocess with pytest-subprocess - 2023-03-08

For apple-notes-to-sqlite I needed to write some tests that simulated executing the osascript command using the Python subprocess module. …

pytest Show files opened by pytest tests - 2022-12-11

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

pytest Mocking a Textract LimitExceededException with boto - 2022-08-07

For s3-ocr issue #21 I needed to write a test that simulates what happens when Amazon Textract returns a "LimitExceededException". When using boto this error presents itself as an exception: …

pytest Using pytest and Playwright to test a JavaScript web application - 2022-07-24

I decided to add automated tests to my Datasette Lite project. Datasette Lite bundles my Datasette Python web application as a client-side application running inside WebAssembly using Pyodide. …

pytest Treating warnings as errors in pytest - 2022-04-01

I was seeing this warning in a Django project when I thought I was correctly using timezone-aware dates everywhere: …

pytest Async fixtures with pytest-asyncio - 2022-03-19

I wanted to use a fixture with pytest-asyncio that was itsef as async def function, so that it could execute await statements. …

pytest pytest coverage with context - 2022-03-04

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. …

pytest Opt-in integration tests with pytest --integration - 2022-01-26

For both s3-credentials and datasette-publish-fly I have a need for real-world integration tests that actually interact with the underlying APIs (AWS or Fly) to create and destroy resources on those platforms. …

pytest Testing a Click app with streaming input - 2022-01-09

For sqlite-utils#364 I needed to write a test for a Click app which dealt with input streamed to standard input. I needed to run some assertions during that process, which ruled out the usual CliRunner.invoke() testing tool since that works by running the command until completion. …

pytest Writing pytest tests against tools written with argparse - 2022-01-08

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. …

pytest Quick and dirty mock testing with mock_calls - 2021-11-02

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

pytest Using VCR and pytest with pytest-recording - 2021-11-02

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 to be replayed during the tests. …

pytest Start a server in a subprocess during a pytest session - 2020-08-31

I wanted to start an actual server process, run it for the duration of my pytest session and shut it down at the end. …

pytest Code coverage using pytest and codecov.io - 2020-08-15

I got my asgi-csrf Python package up to 100% code coverage. Here's the pull request. …

pytest Registering temporary pluggy plugins inside tests - 2020-07-21

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

pytest Asserting a dictionary is a subset of another dictionary - 2020-05-28

My lazy approach to writing unit tests means that sometimes I want to run an assertion against most (but not all) of a dictionary. …

pytest How to mock httpx using pytest-mock - 2020-04-29

I wrote this test to exercise some httpx code today, using pytest-mock. …

pytest Session-scoped temporary directories in pytest - 2020-04-26

I habitually use the tmpdir fixture in pytest to get a temporary directory that will be cleaned up after each test, but that doesn't work with scope="session" - which can be used to ensure an expensive fixture is run only once per test session and the generated content is used for multiple tests. …