Better Python tests with inline-snapshot
16 points by a5rocks
16 points by a5rocks
One of the reasons I love python is because it’s so powerful and flexible that it’s easy to write this sort of thing yourself. The last time i saw a snapshot testing thread here i was inspired to write my own. Its around 10 lines of python and I’ve been using it ever since.
This is tedious to write and a pain to maintain. It's also not very thorough - you have no idea what other fields you're silently ignoring.
these are two separate concerns, and I would love to see an enhancement that let me write something like {'a': 1, _: ...} to say "I don't care about the rest of the container, but test and update this subset of it".
I'm not 100% certain, but you might be able to do something like this with the structural pattern-matching constructs added in Python 3.10.
I've done this (not using this library, but with other snapshot libraries) by snapshotting {"a": myObject.a} or similar (with a function to do this if there are multiple cases where I want it).
does that match if there are extra keys (e.g. I want to match {'a': 1, 'b': 2, 'c': 3} with only the 'a' key specified in the snapshot)
Yeah, you're just constructing a new object that only contains the parts of the old object you care about snapshotting.
Neat idea, but I wouldn't use this:
Snapshot tests are so underrated! Love them. Though I slightly prefer when they’re not inline but stored in a separate file
In the first example, why wouldn't you test just the fields you're interested in? Why is it important to include rest of the object contents that get autogenerated anyway?