Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift Assertions in Xcode 9.1 #478

Closed
blessingLopes opened this issue Nov 3, 2017 · 16 comments
Closed

Swift Assertions in Xcode 9.1 #478

blessingLopes opened this issue Nov 3, 2017 · 16 comments
Assignees

Comments

@blessingLopes
Copy link

What did you do?

I used expect { () -> Void in fatalError() }.to(throwAssertion()) in a test, which
throws a fatal error, completely stops the tests suite execution, and we get a

expected to raise any exception, got no exception

test failed message

What did you expect to happen?

expect { () -> Void in fatalError() }.to(throwAssertion()) to succeed.

What actually happened instead?

expect { () -> Void in fatalError() }.to(throwAssertion()) fails.

Environment

List the software versions you're using:

  • Nimble: 7.0
  • Xcode Version: 9.1 (9B55)
  • Swift Version: 4.0

Please also mention which package manager you used and its version. Delete the
other package managers in this list:

  • Cocoapods: 1.3.1

Maybe I'm missing something or doing something wrong, my deep apologies if that's the case.
Thanks for any feedback!

@szweier
Copy link

szweier commented Nov 3, 2017

Seeing the same issue seems to be Xcode 9.1 specific.

@VojtaStavik
Copy link

VojtaStavik commented Nov 6, 2017

This is happening only when the test suite is run in the host application. For standalone tests everything works OK.

@bryantjustin
Copy link

You can continue the tests by clicking on "Continue program execution" but this gets tedious if you have a few of these type of tests.

@msewell
Copy link

msewell commented Nov 7, 2017

As a sort-of workaround for getting stuck on debugger breakpoints for fatal errors when running tests, you can run your tests without debugging support. Disable 'Debug Executable' in your scheme by going to Edit Scheme > Test > Info and unchecking 'Debug Executable'.

@bryantjustin
Copy link

@msewell Thanks for the tip. I just confirmed it and turning off "Debug Executable" stops this from happening. I guess if we need to actually have the debugger up for a test, we'll probably want to isolate using fdescribe(), fcontext(), or fit() to avoid having to go through all the fatalErrors.

@blessingLopes
Copy link
Author

Many thanks for the feedback and useful suggestions!

@ghost
Copy link

ghost commented Dec 13, 2017

Maybe, you should mention this in documentation? Solution was not obvious for me.

@msewell
Copy link

msewell commented Dec 15, 2017

@MortyMerr: Disabling debugging during test runs isn't really a solution (it hardly even is a workaround). This issue still needs a "real" fix.

@rafaelnobrepd
Copy link

A side effect of disable debugging during tests (which is quite inconvenient as is), is that it seems to affect coverage reports (always reports 0% when debug executable is disabled).
Unfortunately its a real pain, to the point that I'm considering removing the assertion expectations

@mvandervelden
Copy link

Haven't had this issue before, until I started using Xcode 9.3 / Swift 4.1, where it fails consistently. Can confirm that disabling debugging during tests works, but as mentioned before, it's not a suitable workaround.

@RetepV
Copy link

RetepV commented Apr 12, 2018

Woops. And I am finding this issue as well, after having upgraded to Xcode 9.3.

            it("Assertion from inch unit test") {
               expect {
                    try HeightConverter.stringForProfileHeightUnit(HeightUnit.inch)
                }.to(throwAssertion())
            }

Code to test executes the following assert:

            assert(false, "Profile only defines cm or ft. Anything else is a bug.")

In Xcode 9.2, the test pass fine. In Xcode 9.3, the line actually asserts and the test run fails because it detects an assertion (which it's actually supposed to detect).

@drekka
Copy link

drekka commented May 4, 2018

Hitting this with 9.3 as well.

It appears from my testing that Apple has modified the way the debugger handles program exits and is aggressively halting at the point where the code exits. Thus making it impossible for Nimble to trap it.

Turning off debugging of the executable does indeed halt the problem but is not really a solution as it effectively disables test debugging.

I'm not sure that this can be addressed either with Nimble or XCTest functionality.

@danielaRiesgo
Copy link

Hello. Just wanted to know if there is an update about this?

I'm having the same problem. Worked in XCode 9.2 but not in XCode 9.3.

@drekka
Copy link

drekka commented Jun 5, 2018

Problem is persisting in Xcode 9.4. I suspect this will not be fixed and Nimble will have to find an alternative solution. For myself, I've gone with the approach of using a local override of fatalError(...) and then adding code to set a mock equivalent during testing. Unfortunately I cannot publish the code here as I did it for a client and they would have to approve it (unlikely!), however I can say it was based on https://medium.com/@marcosantadev/how-to-test-fatalerror-in-swift-e1be9ff11a29

@ikesyo
Copy link
Member

ikesyo commented Jun 7, 2018

Looks like we can adopt mattgallagher/CwlPreconditionTesting@ce96cb1 to address the issue.

Related to swiftlang/swift#11329.

@ikesyo
Copy link
Member

ikesyo commented Jun 27, 2018

Fixed by #545.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests