Unit Test Fatalerror in Swift

Unit Test fatalError in Swift

Nimble ("A Matcher Framework for Swift and Objective-C") got your back :

Swift Assertions

If you're using Swift, you can use the throwAssertion matcher to check if an assertion is thrown (e.g. fatalError()). This is made possible by @mattgallagher's CwlPreconditionTesting library.

// Swift

// Passes if 'somethingThatThrows()' throws an assertion,
// such as by calling 'fatalError()' or if a precondition fails:
expect { try somethingThatThrows() }.to(throwAssertion())
expect { () -> Void in fatalError() }.to(throwAssertion())
expect { precondition(false) }.to(throwAssertion())

// Passes if throwing an NSError is not equal to throwing an assertion:
expect { throw NSError(domain: "test", code: 0, userInfo: nil) }.toNot(throwAssertion())

// Passes if the code after the precondition check is not run:
var reachedPoint1 = false
var reachedPoint2 = false
expect {
reachedPoint1 = true
precondition(false, "condition message")
reachedPoint2 = true
}.to(throwAssertion())

expect(reachedPoint1) == true
expect(reachedPoint2) == false

Notes:

  • This feature is only available in Swift.
  • It is only supported for x86_64 binaries, meaning you cannot run this matcher on iOS devices, only simulators.
  • The tvOS simulator is supported, but using a different mechanism, requiring you to turn off the Debug executable scheme setting for your tvOS scheme's Test configuration.

how to stop Xcode iOS unit tests if a fatalerror is hit?

There is no easy fix for this. In case of an uncaught (objc) exception or a failed assert the process that runs the unit tests did receive either a mach exception or a unix signal.

Matt Gallagher has a nice solution for this, which he presents in this blog post.

SwiftUI & Unit test fails: Can't add the same store twice when using core data stack

There's a mistake in your code, when you append to the array of descriptions you now have 2 descriptions.

Change it to:

let description = container.persistentStoreDescriptions.first!
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true

// Load

Swift unit test not stopping after XCTFail() has been called

You can set continuesAfterFailure to false. Then the test runner stops when one test fails.

Swift Fatal error: … Expected some type but found some type

I could reproduce your error in a sample project where the managed object class definition file was added both to the application target and the unit tests target. This also gave some console warnings when running tests about the class being implemented in two places.

Is this what is happening in your case?

Sample Image

The code should only be present in your app target, and you should use @testable import ShopEasy to access app code in your unit tests.

Can I stop XCode from breaking on a fatalError/preconditionFailure?

Looks like this is fixed in Nimble 7.1.3, a library which I use to test these particular cases. I also found this specific issue in the Nimble library issue 478 which has a comment on a workaround you can use to disable the debug executable when running you tests. Hopefully this helps anyone else stuck with the same problem I had.



Related Topics



Leave a reply



Submit