Aka*_*ndu 5 xcode unit-testing ios xctest swift
我正在使用 XCTests 在 Xcode 中运行一些单元测试。当我运行它们时,所有测试要么通过,要么失败,并显示小绿色对勾或红十字。 主要问题似乎是我的测试过早取消并且它们没有正常运行。
以下是更多信息:
运行我的测试时,它完成编译,然后很快在 Xcode 的信息栏中说“正在取消...”(顶部和居中的栏,在您构建项目时显示警告和“正在构建”)。
我正在使用 .xcworkspace 文件(因为我使用的是 Pod)
在遇到任何这些问题之前,我只是在编写测试并尝试不同的编写测试方法。我已经恢复了所有这些更改,但仍然出现这些错误。
在没有模拟器的情况下运行测试时,我也发现了同样的问题。
当我从 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
问题在评论中解决。对于未来的读者,如果您遇到类似问题,请检查以下一些好东西:
| 归档时间: |
|
| 查看次数: |
2133 次 |
| 最近记录: |