【问题标题】:Why does this Swift unit test crash when run in parallel with other tests?为什么这个 Swift 单元测试在与其他测试并行运行时会崩溃?
【发布时间】:2017-03-27 20:24:51
【问题描述】:

我有一个单元测试,当它单独运行时成功,但在与其他人一起运行时崩溃EXC_BAD_ACCESS(大部分时间)waitForExpectations测试。

func testStartMonitoring() {

    let mockLocationManager = MockLocationManager()
    let beaconListener = BeaconListener(locationManager: mockLocationManager, uuid: BeaconListenerTests.defaultUUID)

    let e = self.expectation(description: "Expected beacons to be detected")

    //If the listener calls back, the expectation succeeds.
    beaconListener.didReceiveNewRoomInformation = { data in

        //There are three entries in the test data
        XCTAssert(data.count == 3)

        e.fulfill()
    }

    //Start listening
    beaconListener.startListening()

    //Wait up to 15s for a response
    self.waitForExpectations(timeout: 15.0, handler: {error in
        if let error = error {
            print("\(error)")
        }
    })
}

  • 我没有其他异步测试
  • 测试永远不会因为超时而失败
  • 由于测试仅在某些时候崩溃,我预计问题是某个地方的竞争条件,但我不确定从哪里查看。

我也可以用更简单的代码重现这个:

func testStartMonitoring() {

    let e = self.expectation(description: "Expected beacons to be detected")

    let deadlineTime = DispatchTime.now() + .seconds(1)
    DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
        e.fulfill()
    }

    //Wait up to 15s for a response
    self.waitForExpectations(timeout: 15.0, handler: {error in
    })
}

我从命令行运行了测试,发现了这条额外的信息:

Error Domain=IDETestOperationsObserverErrorDomain Code=5 "Early unexpected exit, operation never finished bootstrapping - no restart will be attempted" UserInfo={NSLocalizedDescription=Early unexpected exit, operation never finished bootstrapping - no restart will be attempted}

其他一些答案表明这可能是由系统警报引起的。这是可以理解的,我正在使用需要权限警报的位置服务。但是,我正在运行测试的设备已经接受了权限,因此不应显示警报。

【问题讨论】:

  • testStartMonitoring() 不会在我的系统中崩溃
  • @Anish웃,您是否将它与其他测试一起运行?这似乎是问题的症结所在。
  • 是的...事实上我将该功能复制到另一个文件中并测试了项目..根本没有崩溃..
  • 我不知道到哪里去找问题。有什么建议吗?
  • Here 它说“此方法在测试流程中创建一个同步点。在任何给定时间只能激活一个 waitForExpectations(timeout:handler:)”

标签: swift unit-testing xctest xctestexpectation


【解决方案1】:

我遇到了类似的问题 - 一组测试有多个 expectations 崩溃 - 并遇到了您的问题。它让我意识到这是导致问题的权限——这里是位置管理器,而语音识别是我的。所以我嘲笑了我的授权请求类并注入了积极的回应。

您必须搜索调用任何需要许可的方法 - 它们可能是也可能不是带有expectations 的方法。

【讨论】:

  • 我在弹出照片权限对话框时也遇到了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
相关资源
最近更新 更多