Skip to content

Running test repeatedly results in duplicate snapshot being created. #577

Description

@Deco354

Describe the bug
Running a snapshot test repeatedly with xcode's new 'run test repeatedly' functionality results in additional snapshots being created for that test.

We've a wrapper around this library which I've posted below although the identifier we're passing in to the testName does not include the iteration number at the real testSnapshot callsite.

    public func testSnapshot(_ view: UIView,
                             identifier: String = #function,
                             snapshotType: SnapshotType = .image,
                             precision: CGFloat = 0.95,
                             file: StaticString = #file,
                             line: UInt = #line,
                             waiting: TimeInterval = 0,
                             isArchitectureDependent: Bool = false) {
        semaphore.wait()
        var testID = identifier
        if isArchitectureDependent && (Architecture.isArm64 || Architecture.isRosettaEmulated) {
            testID.append("-arm64")
        }
        
        switch snapshotType {
        case .image:
            assertSnapshot(matching: view,
                           as: .wait(for: waiting, on: .image(precision: Float(precision))),
                           record: isRecording,
                           file: file,
                           testName: testID,
                           line: line)
            
        case .recursiveDescription:
            assertSnapshot(matching: view,
                           as: .wait(for: waiting, on: .recursiveDescription()),
                           record: isRecording,
                           file: file,
                           testName: testID,
                           line: line)
        }
        semaphore.signal()
    }

To Reproduce
Take any snapshot test right click on it and run "test repeatedly". At a certain iteration a number matching this iteration (2 in the screenshots below) will be appended to the snapshot reference image name causing an additional snapshot to be saved and the tests to fail.
image

image

Expected behavior
I'd expect the test to use only one snapshot no matter how many time they're run.

Environment

  • swift-snapshot-testing version [e.g. 1.8.1]
  • Xcode 13.2
  • Swift 5.5
  • OS: iOS 15

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions