uber / ios-snapshot-test-case

Snapshot view unit tests for iOS
MIT License
1.79k stars 211 forks source link

Can't get it to work #191

Closed filippopassante closed 10 months ago

filippopassante commented 10 months ago

I've specified the example keys and values in the run scheme's environment variables, run the tests in record mode and then with record mode off.

Still, I'm getting the error "/Users/xqc/Library/Mobile Documents/com~apple~CloudDocs/Xcode Projects & Resources/Snapshot/SnapshotTests/SettingsVCSnapshotTests.swift:26: error: -[SnapshotTests.SettingsVCSnapshotTests test_example] : failed - Snapshot comparison failed: Optional(Error Domain=FBSnapshotTestControllerErrorDomain Code=1 "Unable to load reference image." UserInfo={NSLocalizedFailureReason=Reference image not found. You need to run the test in record mode, NSLocalizedDescription=Unable to load reference image., FBReferenceImageFilePathKey=/Users/xqc/Library/Developer/CoreSimulator/Devices/A5B479B7-4DF1-B030-05CAF050B00C/data/Containers/Bundle/Application/629A7F7C-4264-B830/Snapshot.app/PlugIns/SnapshotTests.xctest/ReferenceImages_64/SnapshotTests.SettingsVCSnapshotTests/test_example@3x.png})"

`import iOSSnapshotTestCase @testable import Snapshot

final class SettingsVCSnapshotTests: FBSnapshotTestCase { private var sut: SettingsVC!

override func setUp() {
    super.setUp()
    recordMode = false
    sut = SettingsVC()
}

override func tearDown() {
    sut = nil
    super.tearDown()
}

func test_example() {
    FBSnapshotVerifyViewController(sut)
}

}`

What can I do to figure out the problem?

Thanks

backslash-f commented 10 months ago

Yep, same issue:

import iOSSnapshotTestCase
import XCTest

@testable import AssertYourself

final class SnapshotTests: FBSnapshotTestCase {

    private var sut: SnapshotsViewController!

    @MainActor
    override func setUp() {
        super.setUp()

        recordMode = false

        let sb = UIStoryboard(name: "Main", bundle: nil)
        let identifier = String(describing: SnapshotsViewController.self)
        sut = sb.instantiateViewController(identifier: identifier)
        sut.loadViewIfNeeded()
    }

    override func tearDown() {
        sut = nil
        super.tearDown()
    }

    func test_zero() throws {
        FBSnapshotVerifyViewController(sut)
    }
}

When I run it with recordMode = true, it generates the image (the image is there, I checked):

2023-11-12 23:49:17.386959+0100 AssertYourself[34842:868152] Reference image save at: /Users/fernando.fernandes/Library/Developer/CoreSimulator/Devices/FA8F811E-B1FF-45FF-995A-ED6F1D93705A/data/Containers/Bundle/Application/0454BE61-CE78-4B5E-80F6-36F75C663A9C/AssertYourself.app/PlugIns/SnapshotTests.xctest/ReferenceImages_64/SnapshotTests.SnapshotTests/test_zeroAndReturnError_@3x.png

But then, running again with recordMode = false produces:

/Users/fernando.fernandes/Downloads/source/backslash-f/assert-yourself/Xcode/Tests/SnapshotTests/SnapshotTests.swift:35: error: -[SnapshotTests.SnapshotTests test_zero] : failed - Snapshot comparison failed: Optional(Error Domain=FBSnapshotTestControllerErrorDomain Code=1 "Unable to load reference image." UserInfo={NSLocalizedFailureReason=Reference image not found. You need to run the test in record mode, NSLocalizedDescription=Unable to load reference image., FBReferenceImageFilePathKey=/Users/fernando.fernandes/Library/Developer/CoreSimulator/Devices/FA8F811E-B1FF-45FF-995A-ED6F1D93705A/data/Containers/Bundle/Application/FE8C6E31-88AD-4B07-8F22-70E848CCD6AF/AssertYourself.app/PlugIns/SnapshotTests.xctest/ReferenceImages_64/SnapshotTests.SnapshotTests/test_zeroAndReturnError_@3x.png})

It seems the directories don't match the ones set with environment variables:

dirs

Environment: Xcode 15.0.1 (15A507) MacBook Pro 13-inch, M1, 2020 MacOS Sonoma 14.1.1 (23B81)

backslash-f commented 10 months ago

Oh, I get it... When using test plans, the environment variables should be set into the test plan configuration, not the app scheme:

test_plan

That did the trick on my side. 👍🏻

filippopassante commented 10 months ago

Thank you so much