swift-snapshot-testing icon indicating copy to clipboard operation
swift-snapshot-testing copied to clipboard

Add support for tests executed repeatedly

Open krzysztofpawski opened this issue 3 years ago • 9 comments

Motivation

Xcode 13 introduced new parameters to xcodebuild that allow to run tests repeatedly (wwdc talk). Unfortunately SnapshotTesting doesn't handle such situation in a correct way.

Proposal

We can use XCTestObservation in order to clean counter map between different test cases.

Resolved issues

Closes #577

krzysztofpawski avatar Mar 23 '22 10:03 krzysztofpawski

I have two questions regarding this

  1. Would we still have SnapshotTesting increment the counter for multiple snapshots inside one test case? e.g. snapshot assertion from different lines but the same test case
  2. I'm using Quick/Nimble for this and if the test originates from the same line, i.e. a toEventually expectation, would it use the same file name for each execution?

tahirmt avatar Mar 25 '22 19:03 tahirmt

I have two questions regarding this

  1. Would we still have SnapshotTesting increment the counter for multiple snapshots inside one test case? e.g. snapshot assertion from different lines but the same test case

Yes. TestObserver is called after test case is executed so the feature of counting multiple assertSnapshot calls works as expected.

  1. I'm using Quick/Nimble for this and if the test originates from the same line, i.e. a toEventually expectation, would it use the same file name for each execution?

I've never used Quick or Nimble. Would you be so kind to check with my forked version if it works with your project? I've already checked with our where we have planty of different scenarios but definitely would be cool to have confirmation also from someone else.

krzysztofpawski avatar Mar 28 '22 06:03 krzysztofpawski

@krzysztofpawski I tried it out and it still produces many snapshots. The way Quick works is that it executes the same condition with a polling until it passes.

tahirmt avatar Mar 28 '22 18:03 tahirmt

Maybe it will make sense to make it unique based on the test line. The only thing that would break it is if someone adds two assertions on the same line

tahirmt avatar Mar 29 '22 21:03 tahirmt

@tahirmt could you check it one more time? I found one more case when it was producing too many reference files in our repo. If not maybe then you could prepare a very simple project reproducing the issue mentioned by you and attach it here?

krzysztofpawski avatar Mar 30 '22 06:03 krzysztofpawski

@krzysztofpawski I created a demo repo to test this using your fork https://github.com/tahirmt/test-toeventually-behavior-snapshots.

tahirmt avatar Mar 30 '22 11:03 tahirmt

@tahirmt I'm afraid it can't be fixed in swift-snapshot-testing library without some bigger refactor. But the solution I see for you is that you can use named argument (pass some non empty string) to omit this counting mechanism. It's not perfect but it should do the work for your matcher.

krzysztofpawski avatar Mar 30 '22 17:03 krzysztofpawski

@tahirmt I'm afraid it can't be fixed in swift-snapshot-testing library without some bigger refactor. But the solution I see for you is that you can use named argument (pass some non empty string) to omit this counting mechanism. It's not perfect but it should do the work for your matcher.

That's what I was thinking too. I was thinking of many alternatives but I can't think of a better solution for now. Thanks for looking into it though.

tahirmt avatar Mar 30 '22 17:03 tahirmt

@stephencelis any possibility to review this PR?

krzysztofpawski avatar May 11 '22 15:05 krzysztofpawski