SwissLife-OSS / snapshooter

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

Reuse Snapshot across multple tests. #116

Open s-tarasov opened 3 years ago

s-tarasov commented 3 years ago

Sometimes multiple tests produce equal snapshots... And it needs to be checked.

How about add possiblity to match against different test snapshot or common snapshot?

RohrerF commented 3 years ago

I think you can already by setting the snapshot name explicitly

example


[Fact]
public void SomeTest()
{
    // arrange
    var serviceClient = new ServiceClient();

    // act
    TestPerson person = serviceClient.CreatePerson(
        Guid.Parse("1192F21C-8501-4771-A070-C79C7C7EF411"), "Albert", "Einstein");

    // assert

    // Snapshot name is MyCommonSnap.snap
    Snapshot.Match(person, "MyCommonSnap");
}

[Fact]
public void SomeOtherTest()
{
    // arrange
    var serviceClient = new ServiceClient();

    // act
    TestPerson person = serviceClient.CreatePerson(
        Guid.Parse("1192F21C-8501-4771-A070-C79C7C7EF411"), "Albert", "Einstein");

    // assert

    // Snapshot name is MyCommonSnap.snap
    Snapshot.Match(person, "MyCommonSnap");
}
s-tarasov commented 3 years ago

Thank you for this example! Little problem here... Two tests write single mismatch snapshot.

Will be enough one test that writes.

        [Fact]
        public void SomeTest()
        {
            // arrange
            var serviceClient = new ServiceClient();

            // act
            TestPerson person = serviceClient.CreatePerson(
                Guid.Parse("1192F21C-8501-4771-A070-C79C7C7EF411"), "Albert1", "Einstein");

            // assert

            // Snapshot name is MyCommonSnap.snap
            Snapshot.Match(person, "MyCommonSnap");
        }

        [Fact]
        public void SomeOtherTest()
        {
            // arrange
            var serviceClient = new ServiceClient();

            // act
            TestPerson person = serviceClient.CreatePerson(
                Guid.Parse("1192F21C-8501-4771-A070-C79C7C7EF411"), "Albert2", "Einstein");

            // assert

            // Snapshot name is MyCommonSnap.snap
            Snapshot.Match(person, "MyCommonSnap", createMismathSnapshot: false);
        }
    }
RohrerF commented 3 years ago

@s-tarasov how would your desired solution look like?

Something like this would be possible but is ugly IMO since you would have to rename the snapshot manually when you wan't to override the current snapshot. /mismatch/

s-tarasov commented 3 years ago

I sugest add new option createMismathSnapshot with default vaue true. In main test: Snapshot.Match(person, "MyCommonSnap"); In secondary tests: Snapshot.Match(person, "MyCommonSnap", createMismathSnapshot: false);

/mismatch/

Second soultion add new option mismatchSnapSufix. In main Test: Snapshot.Match(person, "MyCommonSnap"); In secondary Tests: Snapshot.Match(person, "MyCommonSnap", mismatchSnapSufix:"SecondaryTest");

/mismatch/