ios - 在 Xcode 9 GM 中运行单元测试时出现神秘的 NSInternalInconsistencyException

标签 ios xcode xctest xcode9-beta xcode9

我在 Xcode 9 GM 上运行我的 iOS 应用程序的单元测试,其中几个失败并出现奇怪的 NSInternalInconsistencyException,提示无法报告某些测试断言,因为牵连测试没有关联的 XCTestRun 对象。我正在使用 OCMockito + OCHamcrest 进行模拟和调用验证。

为了演示目的,假设我的应用名为 MyTestApp,并且我有一个测试类 FooTest(继承自 XCTestCase)。在 -setUp 中,我为测试创建并连接了各种模拟对象,在 -tearDown 中我将它们设置为 nil 以防万一。

这是我遇到的异常示例:

2017-09-19 13:23:01.852729-0700 xctest[17006:5392130] *** Assertion failure in -[FooTest recordFailureWithDescription:inFile:atLine:expected:], /Library/Caches/com.apple.xbs/Sources/XCTest_Sim/XCTest-13201/Sources/XCTestFramework/Core/XCTestCase.m:308
/Users/fooUser/Workspaces/MyTestApp/tests/FooTest.mm:46: error: -[FooTest testSomething] : failed: caught "NSInternalInconsistencyException", "Unable to report test assertion failure 'Expected 1 matching invocation, but received 0' from /Users/fooUser/Workspaces/MyTestApp/tests/FooTest.mm:135 because it was raised inside test case -[FooTest testSomethingElse] which has no associated XCTestRun object. This may happen when test cases are constructed and invoked independently of standard XCTest infrastructure."
(
    0   CoreFoundation                      0x000000010255d1cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x0000000101ebff41 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000102562362 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x0000000101827089 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
    4   XCTest                              0x0000000101d96875 -[XCTestCase recordFailureWithDescription:inFile:atLine:expected:] + 518
    5   MyAppUnitTests                     0x000000011d7f2f5a -[HCXCTestFailureReporter executeHandlingOfFailure:] + 154
    6   MyAppUnitTests                     0x000000011d7f2b73 -[HCTestFailureReporter handleFailure:] + 67
    7   MyAppUnitTests                     0x000000011d64ec9a MKTFailTest + 217
    8   MyAppUnitTests                     0x000000011d64c3f4 -[MKTExactTimes verifyData:] + 254
    9   MyAppUnitTests                     0x000000011d64f2ba -[MKTBaseMockObject verifyInvocation:usingVerificationMode:] + 137
    10  MyAppUnitTests                     0x000000011d64f20b -[MKTBaseMockObject handlingVerifyOfInvocation:] + 115
    11  MyAppUnitTests                     0x000000011d64f15a -[MKTBaseMockObject forwardInvocation:] + 64
    12  CoreFoundation                      0x00000001024dfed8 ___forwarding___ + 760
    13  CoreFoundation                      0x00000001024dfb58 _CF_forwarding_prep_0 + 120
    14  MyAppUnitTests                     0x000000011c40b486 -[FooTest setUp] + 294
    15  XCTest                              0x0000000101d97b39 __24-[XCTestCase invokeTest]_block_invoke_3 + 31
    16  XCTest                              0x0000000101d97809 __24-[XCTestCase invokeTest]_block_invoke + 271
    17  XCTest                              0x0000000101ddff45 -[XCUITestContext performInScope:] + 183
    18  XCTest                              0x0000000101d976ef -[XCTestCase invokeTest] + 141

注意事项:

  • 单元测试不会初始化 XCTestCase 或 XCTest 框架之外的任何 XCTest“基础结构”类。
  • 受影响的单元测试的其余语句实际上得到执行,直到出现异常为止。
  • 隐含的代码行如下所示:[verify(_mockTestObject) description];
  • 堆栈跟踪位于问题发生的位置 - 它说它在 -setUp 中,但据我所知,OCMockito 只是在 next 单元测试的运行。问题位置 (FooTest.mm:135) 看起来像上面的 verify() 调用。

事实上,所有因该异常而失败的单元测试都失败了,因为我们试图验证 -[NSObject description] 是否已在一个模拟对象或另一个模拟对象上被调用。删除所有验证该调用的实例使测试通过。

我在 Google 上搜索了此特定 NSInternalInconsistencyException 的其他实例,但没有产生任何结果。我不确定 -[NSObject description] 与可能在模拟对象上调用的任何其他方法有何不同 - 问题可能也不存在,但它只是以这种方式表现出来。我还试图找到是否有任何可以为 XCTest 打开的调试/诊断选项,这样我可能可以获得有关测试执行的更详细的日志记录信息,但我也没有找到类似的东西。

关于我下一步应该看哪里有什么想法吗?非常感谢!

最佳答案

我在处理一些大型遗留代码测试时遇到了类似的问题。问题是:使用 XCTExpectation 的测试在单独运行时是否工作正常?如果是这样,这意味着一些在之前执行的测试 NSInternalInconsistencyException 正在以执行它们的方式分派(dispatch) XCTest 相关方法相关测试完成后。

它看起来像(示例):

Test1 -> 异步分派(dispatch)执行 XCTFail

的“ block ”

测试 1 -> 完成

XCTFail 在主线程或其他线程上执行(但 Test1 通过,因为它没有“失败”地完成)。

Test2 -> 使用 XCTExpectation 测试一些东西 -> NSInternalInconsistencyException

Apple 文档没有提供太多关于 XCTest 内部结构的信息,但我很确定这就是问题所在。尝试按照故障排除步骤来确定“冲突”测试(在没有 XCTestExpectation 的情况下执行异步操作的错误测试,例如使用带有最终执行 XCTest 断言的完成处理程序的方法,失败等):

  1. 在您的套件中进行二进制搜索:禁用一半测试并使用您的 FooTest 运行套件。
  2. 如果您的测试套件运行良好,请重新启用一半已禁用的测试。如果测试套件运行异常,则重新启用所有禁用的测试并禁用另一半。
  3. 然后针对剩余的少量测试重复步骤 1。
  4. 最后,您将完成导致此异常的测试。

在我的案例中,有多个测试导致了与 XCTestExpectation 的冲突,因此搜索非常麻烦(一套 1000 多个 XCTestCases 需要几个小时,所以大约 5k测试)。

然后彻底调查测试中发生的与您的测试冲突的情况。

关于ios - 在 Xcode 9 GM 中运行单元测试时出现神秘的 NSInternalInconsistencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46309557/

相关文章:

html - 移动 iOS Safari : Percentage height inside of a vh height container

ios - 执行优先级非常低的代码

ios - 从 SwiftUI 中的 ViewModel 访问 EnvironmentObject

xcode - tvos:Alamofire 框架的 iTunesConnect 验证失败:不包含位码

ios - 无法插入新 socket ?

swift - 如何从字符串中删除一些空格?

ios - 如何使用 CoreData 从选定的 Collection View 单元格中获取标签文本?

ios - 创建一个简单的 XCTestCase,找不到作为 CocoaPod 安装的 ZeroPush.h

swift - Xcode 10.1 中的 .xctest 框架红色

ios - 在 Swift4 中获取错误的 Base64 图像