4

While I am running unit tests with XCTest in Swift, they run fine when code coverage is turned off. However once I try to enable code coverage, I have a failed build/test with 4 classes giving the following error message: Command failed due to signal: Segmentation fault: 11.

fadfad
  • 349
  • 1
  • 5
  • 17
  • Could it be an issue with certificates or build targets? I found this source saying that was the issue when getting seg fault from archiving. Maybe try rebuilding certificates? http://stackoverflow.com/questions/26731198/swift-segmentation-fault-when-compiling-for-archive-from-ios-7 – Cole Feb 14 '16 at 15:52
  • Please, provide more details – Anton Belousov Feb 18 '16 at 16:58

4 Answers4

3

Here is what worked for me (as all the other suggestions did not work in my case). I was getting a segmentation fault 11 on a particular Swift class when trying to run unit tests with code coverage turned ON. It turns out that we had a ternary expression on a class's property like so:

let cellBorder : CGFloat = MyHelperClass.isIPad() ? 10.0 : 6.0

Making it a lazy var fixed the problem:

lazy var cellBorder : CGFloat = MyHelperClass.isIPad() ? 10.0 : 6.0

To be clear, the code compiled and worked fine until we tried turning on code coverage. I also found this Open Radar and this guy's post that outlined the solution. Appears to be an Apple bug.

n8tr
  • 5,018
  • 2
  • 32
  • 33
  • 1
    I had the same problem. `lazy` solves, but this will also solve it: `let cellBorder : CGFloat = MyHelperClass.isIPad() ? CGFloat(10.0) : CGFloat(6.0)` – Dmitry Apr 14 '16 at 17:22
2

Without code, build settings, etc. it's hard to say for sure but one thing you should check is to make sure that you are using a @testable import flag in your unit test classes.

For instance with a project named MyApp at the top of your unit test class you would include with the following import @testable import MyApp.

You also want to check to ensure that you've followed the process for enabling coverage all the way through. That information is documented on Apple's developer portal:

Code Coverage | Apple Developer

wmcbain
  • 1,099
  • 9
  • 16
  • I have followed all those steps already. If the segmentation fault appears when the code coverage is introduced, what does would be a general suggestion about the introduction of it to the project to go about debugging or researching into a bit deeper than I currently am at? – fadfad Jan 06 '16 at 17:12
0

See this bug report on similar issue. https://bugs.swift.org/plugins/servlet/mobile#issue/SR-1825

I got the same error when implementing a protocol which required an optional variable that I implemented as a lazy var.

FrostyL
  • 811
  • 6
  • 9
0

In my case, i was building through the cli running the xcodebuild command with Release configuration and no provitionings configured, when i switched to Debug config build and tests worked just fine