snapshooter icon indicating copy to clipboard operation
snapshooter copied to clipboard

Reuse Snapshot across multple tests.

Open s-tarasov opened this issue 4 years ago • 4 comments

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?

s-tarasov avatar Dec 30 '20 13:12 s-tarasov

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");
}

RohrerF avatar Dec 30 '20 14:12 RohrerF

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);
        }
    }

s-tarasov avatar Dec 31 '20 09:12 s-tarasov

@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/ - MyCommonSnap.SomeTest.snap - MyCommonSnap.SomeOtherTest.snap

RohrerF avatar Mar 01 '21 12:03 RohrerF

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/

  • MyCommonSnap.snap

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

/mismatch/

  • MyCommonSnap.snap
  • MyCommonSnap.SecondaryTest.snap

s-tarasov avatar Mar 04 '21 17:03 s-tarasov