SwissLife-OSS / snapshooter

Snapshooter is a snapshot testing tool for .NET Core and .NET Framework
https://swisslife-oss.github.io/snapshooter/
MIT License
302 stars 29 forks source link

Test input snapshots #185

Open Place1 opened 1 year ago

Place1 commented 1 year ago

Is your feature request related to a problem? Please describe.

Snapshot testing the results of code is quite useful and is the purpose of Snapshooter but I'm interested to propose data input shapshotting as a feature request.

The idea is to re-use the snapshot workflow but for data inputs for tests.

For example, instead of writing/generating test data for a test case you could fetch it from somewhere and have Snapshooter generate a snapshot that can be used for future test executions.

A motivating use-case I have is to test some code that processes data coming out of a big database query.

Creating a mock data set is possible but in our case we already have a big anonymous dataset that our local development + testing environments use and so our unit-tests could leverage this.

Describe the solution you'd like

Here's some pseudocode that describes a possible solution.

public async Task Some_test_case()
{
    // Arrange
    var input = await Snapshot.Create<T>(async () =>
    {
        return await GetData();
    });

    // Act
    var result = await processor.Process(input);

    // Assert
    result.Should().MatchSnapshot();
}

When the test is first run the GetData() method would be called to create the input snapshot, saved under the __snapshots__ folder and the result snapshot created as normal.

During future test case executions the Snapshot.Create(...) method would re-use the input snapshot instead of calling GetData().

If a developer wants to update the input snapshot they'd just delete it from the __snapshots__ folder so that it can be re-created.

I think the concepts all work quite nicely together; the test helps us confirm that for the same input (snapshot) we get the same result (snapshot) given some processing logic (unit under test).

Given input and output snapshots might be used in the same test I think input snapshots could have a default filename extension of .input.snap so that they don't conflict with the current snapshot file name(s)


I'm open to contributing a PR as well!