3

How can I stop Xcode iOS unit tests if a fatalerror is hit?

That is in case I have 10 unit tests, but it happens that the code it calls for unit test number 5 has a coding problem (** coding issue in this case is in the test case and setup code **) and is throwing a fatalError. So in this case the unit testing stops there and does not continue to other test cases in that test class.

(not sure if this is the intended operational / process for good unit testing or not? )

bneely
  • 9,083
  • 4
  • 38
  • 46
Greg
  • 34,042
  • 79
  • 253
  • 454
  • Will you be able provide a sample code for that? And which version of Xcode are you using? I am able to continue my tests here. – Yuchen Apr 04 '16 at 15:28

4 Answers4

8

Try

override func setUp() {
    super.setUp()
    // Put setup code here. This method is called before the invocation of each test method in the class.

    continueAfterFailure = false
}
Oleg Danu
  • 4,149
  • 4
  • 29
  • 47
2

A related problem I had was stopping the unit test with a breakpoint when it fails, so that I can see the issue, without having to click or scroll through tons of test output.

Making a unit test failure breakpoint is simple, but wasn't easy to find.

Xcode 8+ there is a new breakpoint that you can add called a "Test Failure Breakpoint".

  1. Click on Breakpoints (Left Panel ... Command + 7)
  2. Click on + (Bottom left corner)
  3. Click on "Test Failure Breakpoint"

Xcode Unit Test Failure Breakpoint

References

Paul Solt
  • 8,375
  • 5
  • 41
  • 46
1

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.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
-1

It is good practice to write safe code everywhere, including your unit tests.

Regarding your problem, as Oleg Danu replied, you should set continueAfterFailure = false.

The next step is to add following before your test can crash.

var optionalVariable: Int?
XCTAssertNotNil(optionalVariable)

I recommend to add it into setUp()

In this way your test will stop before crashing Xcode, and you don't need to set any breakpoints.

Boris Y.
  • 4,387
  • 2
  • 32
  • 50