从 XCTestCase 访问项目代码 - UI 测试 [英] Access to project code from XCTestCase - UI Test

查看:26
本文介绍了从 XCTestCase 访问项目代码 - UI 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中设置 UI 测试.我正在做一个 UI 测试,试图通过我的应用程序登录提示登录.为了确保在测试启动时显示登录提示,我尝试运行 ServerManager.logout(),它位于项目的代码库中.这将导致在启动时显示登录提示.

I'm trying to setup UI Test in my project. I'm making a UI test that tries to login through my apps login prompt. In order to ensure that the login prompt is shown when the test launches, I'm trying to run ServerManager.logout(), which is in the project's codebase. This will cause the Login prompt to be shown on launch.

import XCTest

class SmokeTest: XCTestCase {

    override func setUp() {
        super.setUp()

        // Put setup code here. This method is called before the invocation of each test method in the class.

        // In UI tests it is usually best to stop immediately when a failure occurs.
        continueAfterFailure = false
        // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
        XCUIApplication().launch()

        // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
        ServerManager.logout()
    }

    func testLogin() {

        let app = XCUIApplication()

        let emailTextField = app.textFields["email textfield"]
        emailTextField.tap()
        emailTextField.typeText("test@test.com")

        let passwordTextField = app.secureTextFields["password textfield"]
        passwordTextField.tap()
        passwordTextField.typeText("12345678")

        let loginButton = app.buttons["log in button"]
        loginButton.tap()
    }
}

我应该如何设置我的项目以访问 ServerManager?

当我在 ServerManager.swift 文件上检查 UITest 目标(称为 DonkeyProductionUITests)的 Target Membership 时,Xcode 开始抱怨该文件中的许多引用未针对 UITest 目标定义.因此,我将项目中所有文件的 Target Membership 添加到 UITest Target,包括所有 CocoaPods.它解决了大多数问题.我仍然有一些奇怪的剩菜:

When I checked on Target Membership for UITest target (called DonkeyProductionUITests) on the ServerManager.swift file, Xcode started complaining that many reference in that file was undefined for the UITest target. So I added Target Membership for all files in my project to the UITest Target including all CocoaPods. It solved most issues. Still I have some weird leftovers:

UITest 目标应该有哪些源文件作为编译源"?

为什么像 UIColorUIViewController 这样的类型突然无法被 Xcode 识别?

Why are types like UIColor and UIViewController suddenly not recognizer by Xcode?

推荐答案

只能在 UnitTests 中通过 @testable import 访问您的项目代码.当您运行 UITests 时,这不起作用,因为在 UITest 期间您的测试类无法访问您的应用程序代码.

Accessing your project's code via @testable import is only possible in UnitTests. When you are running UITests this is not working, because during a UITest your test class cannot access your app's code.

来自 Apple 的文档:

UI 测试在根本上不同于单元测试.单元测试使您能够在应用程序的范围内工作并允许您锻炼具有对应用程序变量的完全访问权限的函数和方法,以及状态.UI 测试以与用户相同的方式测试您的应用程序的 UI无需访问应用程序的内部方法、函数和变量.这使您的测试能够以与用户相同的方式查看应用程序确实,暴露了用户遇到的 UI 问题.

UI testing differs from unit testing in fundamental ways. Unit testing enables you to work within your app's scope and allows you to exercise functions and methods with full access to your app's variables and state. UI testing exercises your app's UI in the same way that users do without access to your app's internal methods, functions, and variables. This enables your tests to see the app the same way a user does, exposing UI problems that users encounter.

如果你想在测试后退出,你必须通过你的应用程序的用户界面来完成:如果你的应用程序中有一个退出按钮,在你的测试结束时导航到那里,让测试 tap() 它.

If you want to logout after a test you have to do it via your app's User Interface: If there is a logout button somewhere in your app, navigate there at the end of your test and let the test tap() it.

这篇关于从 XCTestCase 访问项目代码 - UI 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆