XCTests 提前取消

Aka*_*ndu 5 xcode unit-testing ios xctest swift

我正在使用 XCTests 在 Xcode 中运行一些单元测试。当我运行它们时,所有测试要么通过,要么失败,并显示小绿色对勾或红十字。 主要问题似乎是我的测试过早取消并且它们没有正常运行。

以下是更多信息:

  1. 运行测试时,没有任何以前没有发生的控制台输出。我的意思是我之前确实有预期的控制台输出,现在我没有。
  2. 运行测试时,不需要我解锁 iPhone 即可运行该应用程序。此外,即使我的 iPhone 已解锁,它也不会像以前那样运行应用程序。
  3. 运行我的测试时,它完成编译,然后很快在 Xcode 的信息栏中说“正在取消...”(顶部和居中的栏,在您构建项目时显示警告和“正在构建”)。

  4. 我正在使用 .xcworkspace 文件(因为我使用的是 Pod)

  5. 我有一个构建方案,即项目。
  6. 我遇到了同样的问题,加上绿色检查和红色叉号的问题在运行后没有出现在我的测试中,有时我的测试根本没有出现在测试选项卡中。我通过删除 Podfile.lock 文件、Pods 目录和 .xcworkspace 目录,然后运行 ​​Pod install 来解决这些问题。这样做之后,我遇到了同样的问题,除了那两个问题。
  7. 我尝试在不同的分支上运行新的测试,同样的问题。
  8. 在遇到任何这些问题之前,我只是在编写测​​试并尝试不同的编写测试方法。我已经恢复了所有这些更改,但仍然出现这些错误。

  9. 在没有模拟器的情况下运行测试时,我也发现了同样的问题。

  10. 当我从 Xcode 编译并运行应用程序时,它运行良好。

关于如何解决这个问题的任何建议都会很棒!如果需要,请询问更多详细信息。

这是我运行测试的视频。我建立了一个新项目和新的单元测试目标来展示发生了什么。注意:那里有一个空测试和一个应该总是通过的测试:

在此处输入图片说明

如您所见,问题在于测试未正常运行。绿色和红色的十字不再出现,应该总是通过的测试失败了,没有控制台输出,并且在顶部信息栏中几乎立即说“取消......”。关于为什么会发生这种取消的任何建议都会很棒!

以下是代码设置方式的一些屏幕截图:

在此处输入图片说明 在此处输入图片说明

这也是代码(它只是创建新单元测试后的基本代码,除了我添加了一个应该始终通过的断言):

import XCTest
@testable import example

class exampleTests: XCTestCase {

    override func setUp() {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }

    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
        XCTAssertTrue(true)
    }

    func testPerformanceExample() {
        // This is an example of a performance test case.
        self.measure {
            // Put the code you want to measure the time of here.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

所以,按照@CPR 的建议,我在模拟器上又试了一次,错误基本都没有了。我在物理设备上再次尝试,但错误仍然存​​在注意:我之前在模拟器上尝试过,并得到了相同的错误。从那以后我没有改变任何东西,所以我不确定为什么它现在起作用了。这是在 sim 上运行测试的控制台输出(不确定这是否有帮助,但我还是会添加它):

Test Suite 'All tests' started at 2018-12-06 03:59:53.045
Test Suite 'exampleTests.xctest' started at 2018-12-06 03:59:53.046
Test Suite 'exampleTests' started at 2018-12-06 03:59:53.046
Test Case '-[exampleTests.exampleTests testExample]' started.
Test Case '-[exampleTests.exampleTests testExample]' passed (0.001 seconds).
Test Case '-[exampleTests.exampleTests testPerformanceExample]' started.
/Users/akashkundu/Documents/example/exampleTests/exampleTests.swift:30: Test Case '-[exampleTests.exampleTests testPerformanceExample]' measured [Time, seconds] average: 0.000, relative standard deviation: 115.251%, values: [0.000004, 0.000001, 0.000001, 0.000001, 0.000001, 0.000001, 0.000001, 0.000001, 0.000001, 0.000001], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
Test Case '-[exampleTests.exampleTests testPerformanceExample]' passed (0.348 seconds).
Test Suite 'exampleTests' passed at 2018-12-06 03:59:53.396.
     Executed 2 tests, with 0 failures (0 unexpected) in 0.348 (0.350) seconds
Test Suite 'exampleTests.xctest' passed at 2018-12-06 03:59:53.396.
     Executed 2 tests, with 0 failures (0 unexpected) in 0.348 (0.350) seconds
Test Suite 'All tests' passed at 2018-12-06 03:59:53.397.
     Executed 2 tests, with 0 failures (0 unexpected) in 0.348 (0.351) seconds 
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么在物理设备上运行测试会导致这些错误?

编辑:

这是@CPR 找到的 Xcode 中“消息”选项卡中的错误/日志消息:

在此处输入图片说明

这是文本中的错误/日志代码:

example.app encountered an error (Failed to establish communication with the test runner. (Underlying error: Unable to connect to test manager on 8b441d96d063b3b6abf55b06115441d160e85e67. (Underlying error: kAMDMuxConnectError: Could not connect to the device.)))
Run Code Online (Sandbox Code Playgroud)

-------------------------------------------------- ---------------------

解决方案:

感谢@CPR,解决方案是简单地重新启动设备。另一个教训是使用“消息”选项卡查看日志和错误,这是“断点”选项卡旁边最右侧的选项卡。

他的完整答案在这里

CPR*_*CPR 12

问题在评论中解决。对于未来的读者,如果您遇到类似问题,请检查以下一些好东西:

  1. 检查 Xcode 消息选项卡以查找构建错误。
  2. 根据需要重新启动设备/模拟器。这解决了这种情况下的问题。
  3. 在设备/模拟器上卸载/重新安装应用程序。
  4. 证书/配置文件是否有效?尝试生成新的并重新运行。
  5. 它在模拟器上工作吗?如果是,这可能是设备本身的问题。
  6. 运行代码时是否命中断点?如果是,则这是测试失败。如果不是,问题就在测试运行之前就发生了(就像这里的情况)。