Asserting a dictionary is a subset of another dictionary

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

Take for example an API endpoint that returns something like this:

    "sqlite_version": "3.27.1",
    "name": "Datasette",
    "columns": ["foo", "bar"]

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 changes in the future.


assert {
    "name": "Datasette",
    "columns": ["foo", "bar"]
}.items() <= api_response.items()

The trick here is using expected.items() <= actual.items() to assert that one dictionary is a subset of another.

Here's a recent example test that uses this trick:

Created 2020-05-28T08:09:14-07:00 · Edit