Xcode 7:应用程序测试和 UI 测试之间的鸿沟是不可

时间:2023-05-02
本文介绍了Xcode 7:应用程序测试和 UI 测试之间的鸿沟是不可逾越的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

Xcode 7† 提供了一种直接测试 UI 的新方法,包括新的测试目标iOS UI Testing Bundle"(或OS X").

Xcode 7† has a new way to test your UI directly, including a new testing target "iOS UI Testing Bundle" (or "OS X").

在 UI 测试目标中,似乎没有对构成您的应用的模型或类的内置访问.例如.[UIApplication sharedApplication] 不能从您的 UI 测试中调用.这意味着应用程序测试"和UI 测试"存在于一个可能无法逾越的鸿沟中.

In the UI testing target, it appears there's no built-in access to the model or classes that comprise your App. E.g. [UIApplication sharedApplication] would not be callable from your UI tests. This implies that "app tests" and "UI tests" exist across a possibly unbridgeable chasm.

如上所述这里:

问题是 Xcode 的 UI 测试不允许访问实际的应用程序.

The problem is that Xcode’s UI testing does not allow access to the actual app.

问题:

  1. 这个鸿沟可以弥合吗?如果是这样,详细说明如何使用构建和链接器设置以及可能在 github 上工作的 xcodeproj.
  2. 可以在 Apple 文档中的何处找到关于这种分歧的明确声明.

<小时>

† 在撰写本文时,测试版软件.


† At the time of writing, beta software.

推荐答案

黑盒测试

UI 测试是一个黑盒测试框架.您不必对正在测试的代码的实现有任何了解.

UI Testing is a black-box testing framework. You shouldn't have to know anything about the implementation of the code you are testing.

例如,您应该只关心标签上的值发生变化,而不是控制器将正确的数据传递给视图.您可以从应用用户的角度来考虑 UI 测试.她不在乎您的 ItemsViewController 是如何工作的(甚至它是否存在),那么为什么要进行 UI 测试呢?

For example, you should only care that the value on a label changes, not that the controller passed the right data to the view. You can think of UI Testing from the user of your app's perspective. She doesn't care how your ItemsViewController works (or even that it exists), so why should the UI Tests?

让它工作"

话虽如此,我理解你的沮丧.如果您可以启动一个视图控制器,然后使用 UI 测试并做出断言,那就太好了.但是,从 Beta 5 开始,这是不可能的.

That being said, I understand your frustration. It would be great if you could spin up a view controller and then tap around with UI Testing and making assertions. However, as of Beta 5 this is not possible.

有趣的是,您可以在 UI 测试顶部使用简单的 @testable import ModuleName 创建应用对象的实例.请注意,您实际上无法通过类似 .tap() 的方法与它进行交互,因为它是一个 UI* 类,而不是 XCUI* 一个.

What's interesting is that you can, however, create instances of your app's objects with a simple @testable import ModuleName at the top of your UI Tests. Note that you can't actually interact with it via the .tap()-like methods, as it's a UI* class, not a XCUI* one.

Donut 视为应用程序的模块名称.

Consider Donut to be the application's module name.

import XCTest
@testable import Donut

class DonutUITests: XCTestCase {
    let app = XCUIApplication()

    override func setUp() {
        continueAfterFailure = false
        app.launch()
    }

    func testItemsViewController() {
        let controller = ItemsViewController()
        controller.addItemButton.tap() // <---- UIButton does not respond to tap()!
    }
}

这篇关于Xcode 7:应用程序测试和 UI 测试之间的鸿沟是不可逾越的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!