ios - 执行整个测试套件时单元测试用例失败

标签 ios objective-c swift unit-testing xctestcase

我正在为我的 sqlite 数据库类编写单元测试用例。我在该类中有五个公共(public) API。

我的测试用例如下所示:

+ (void)setUp {  // Note, this is class method and hence called only once during start of this test suite.
    // Code to delete the existing sqlite DB file.
}

- (void)testDBManagerSingletonInstance {
    DBManager *dbMgr = [DBManager getSharedInstance];
    DBManager *dbMgr1 = [[DBManager alloc] init];
    XCTAssertEqualObjects(dbMgr, dbMgr1);
}

- (void)testSaveAndDeleteNicknameAPI {
    // Multiple Assert statements in this test.
}

- (void)testAllAccountStatusAPIs {
    // Multiple Assert statements in this test.
}

每个单元测试均执行无任何错误。但是当整个测试套件执行时它失败了。

也许,我知道失败的根本原因。这是因为当执行整个测试套件时,所有测试都是并行运行的,并且数据库中会同时发生更新-删除。因此,当所有单元测试运行时,它将失败。

但我不知道如何解决这个问题,因为这不是异步的,因此我无法使用 XCExpectation 类。

需要帮助来解决和理解问题。

最佳答案

基于 XCTests 的测试不会并行运行 - 它们是按顺序运行的。引用the docs :

Tests execute synchronously because each test is invoked independently one after another.

由于您显示的代码非常少,因此很难说出真正的问题是什么。您很可能与您的假设很接近 - 您应该改进您的setUp(可能从类版本切换到实例版本)和tearDown方法或引入模拟,并且如果可能的话,在模拟数据库上执行测试。

关于ios - 执行整个测试套件时单元测试用例失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43356226/

相关文章:

ios - 删除 Realm 中的项目时出现错误 "Object has been deleted or invalidated"

swift - 删除 SwiftUI subview 中的大量空白

ios - iOS XCode 模拟器的快照

objective-c - 类方法在 iOS 中如何工作?

ios - 在飞行模式下从 coreData 加载数据时崩溃

objective-c - Objective-C 中的数据结构是什么?

javascript - stringByEvaluatingJavaScriptFromString 似乎并不总是有效

ios - 从 UISearchController 搜索栏快速删除黑色边框

ios - UICollectionView 在 ios 7 中接收到单元格的布局属性

swift - swift 3.0 编译的模块无法在 Swift 3.0.1 中导入