meziantou/Meziantou.Framework

Alternative idea to inline snapshot testing

Closed this issue · 4 comments

Feel free to close this once read but I thought you'd be interested in the ideas in regression testing in CsCheck.
The main idea is to not commit the snapshot but to just inline a hash value.
On a successful run a temp file is created with all the values. On failure the comparison to this file shows the changes.
Kind of a best of all worlds in terms of detail and not committing a large number of big files.

I've used it in a risk department at a bank where they are regressing huge risk reports and had issues with the number and size of large files in the repo plus also the pain of updating these files.
It also has some smart handling of compring floating-point numbers to decimal places or significate figures. Doing this the naive way doesn't work as they can be very close but either side of the rounding.

I've not heard anyone else using it or the ideas.

https://github.com/AnthonyLloyd/CsCheck#regression-testing

I'm not sure to understand how you can compare snapshots if you don't commit them somewhere. But, you can change the serializer for InlineSnapshot to create hashes. Here's an example:

var settings = InlineSnapshotSettings.Default with
{
    SnapshotSerializer = new HashSerializer()
};

// Apply settings globally
InlineSnapshotSettings.Default = settings;

// Apply settings per snapshot
InlineSnapshot.WithSettings(settings).Validate("you data");

class HashSerializer : SnapshotSerializer
{
    public override string? Serialize(object? value)
    {
        var serializedValue = new HumanReadableSnapshotSerializer().Serialize(value);
        return Convert.ToBase64String(SHA256.HashData(Encoding.UTF8.GetBytes(serializedValue)));
    }
}

You would save a local application data file of the snapshot if the hash agreed and there isn't already one. These are not committed to the repo just long lived temporary files that can be deleted any time.
On a hash fail you compare the current snapshot with the local application data file. Can be done on CI server also.

How can you check the previous snapshot if it was created by another developer on another machine? You can only see the hash diff but not the content diff as the previous snapshot file is not on your disk.

If you have never seen a successful hash test then you can checkout a previous good commit and test to create. This never really happens, most often you've made the change that is breaking and have a snapshot from previously running.