我写了如下的单元测试:
class Test445Tests: XCTestCase {
func testPerformanceExample() {
XCTAssert(true)
}
}
测试运行时,将调用ViewController.viewDidLoad!有人可以解释为什么会这样吗?
class ViewController: UIViewController {
static var s = false
override func viewDidLoad() {
super.viewDidLoad()
ViewController.s = true // put the debugger here.
}
}
如果您编写此代码并将调试器放在ViewController上,则可以检查该调试器将在该行上停止。
ViewController
之所以创建实例,是因为您的应用ViewController
在启动时创建了一个,并且您的测试包将您的应用用作其“主机应用”:
当设置如上所示进行配置时,这意味着Xcode通过启动主机应用程序,然后将测试包注入其中,来运行测试包中的测试。
您可以将“主机应用程序”设置更改为“无”:
When the setting is configured as “None”, Xcode runs your test bundle using a non-GUI app called the XCTest agent, and doesn't launch your app, so your app doesn't get the chance to create a ViewController
.
Note that when you set “Host Application” to “None”, your test bundle can no longer access any APIs defined in the application. Any APIs you want to test, you have to move into a framework, and link the test bundle with that framework (in the “Build Phases” tab for the test bundle).
Note also that even when you set “Host Application” to “None”, Xcode still launches a simulator. Your test cases are allowed to use iOS system services that are only available when a simulator is running, and Xcode doesn't know in advance whether your test cases need those services. So it has to launch the simulator first, just in case.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句