Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test input snapshots #185

Open
Place1 opened this issue Aug 30, 2023 · 0 comments
Open

Test input snapshots #185

Place1 opened this issue Aug 30, 2023 · 0 comments

Comments

@Place1
Copy link

Place1 commented Aug 30, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant