iOS为什么单元测试类调用ViewController

我写了如下的单元测试:

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

单元测试什么-简单验证类

为什么在操场和单元测试中未调用GCD调度组通知的原因

模拟单元测试中的行为。为什么验证方法调用有好处?

为什么在单元测试中$ compile(element)(scope)之后调用scope。$ digest()

为什么我可以在单元测试类中访问私有方法

为什么不能将AndroidJUnit4和ActivityTestRule导入单元测试类?

为什么我不能为我的单元测试修补来自不同文件的类?

什么是单元测试?

为什么在PACT测试中需要单元测试?

为什么要进行单元测试和功能测试

为什么在单元测试中对instanceOf的测试失败?

调用 backendserver-angular 的服务类的单元测试

对调用外部服务的类进行单元测试

在单元测试中调用其他类方法

为什么测试类方法首先会导致应引发错误的其他单元测试失败?

为什么?单元测试覆盖率中显示的类即使未添加到测试目标中

什么是对python类进行单元测试的正确方法?

单元测试 - 实体(模型类)是什么双(演员)?

单元测试API调用

为什么不能识别“异步无效”单元测试?

为什么我失败的Ruby单元测试不会失败?

为什么“ getSharedPreferences”在单元测试中返回null?

为什么我的单元测试未能通过switch语句?

为什么与JSONObject相关的单元测试失败?

为什么不println!在Rust单元测试中工作?

AngularJS单元测试,为什么有用?

为什么添加参数后我的单元测试失败?

为什么C ++单元测试在模具程序中失败

为什么在Flutter中将单元测试标记为异步