Cryptic NSInternalInconsistencyException when running unit tests in Xcode 9 GM

I experienced a similar problem while working on some big suite of legacy code tests. The question is: are your tests with XCTExpectation working fine when you run them separately? If so, this means that some of the tests that are executed before your tests with NSInternalInconsistencyException are dispatching XCTest related methods in a way which executes them after the relevant test finishes.

it looks like (example):

Test1 -> dispatches asynchronously "block" which executes XCTFail

Test1 -> finishes

XCTFail executed (but Test1 passes, as it finished without "fail") on the main or other thread.

Test2 -> tests something with XCTExpectation -> NSInternalInconsistencyException

Apple docs don't provide much information about internal guts of XCTest, but I'm pretty sure that this is the issue. Try following troubleshooting steps to pin down tests "conflicting" (the bad ones which do asynchronous stuff without XCTestExpectation, like use methods with completion handlers which are eventually doing XCTest assertions, fails etc.):

  1. Do binary search in your suite: disable half of the tests and run the suite with your FooTest.
  2. If your test suite runs fine re-enable half of the disabled tests. If test suite runs with exception, re-enabled all disabled tests and disable other half.
  3. Repeat step 1 then with respect to smaller amount of tests left.
  4. Finally you will end up with tests which cause this exception.

In my case there were multiple tests causing this conflict with XCTestExpectation, hence the search was quite pesky (several hours for a suite of 1000+ XCTestCases, so around 5k tests).

Then investigate thoroughly what happens in the test which is conflicting with your test.