问题
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.
回答1:
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.
回答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
回答3:
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.
来源:https://stackoverflow.com/questions/34634486/segmentation-fault-11-when-code-coverage-is-turned-on-in-swift