简体   繁体   English

使用XCUI进行UI测试

[英]UI Testing with XCUI

I'm trying for unit testing of view controllers in xCode 8. I'm able to record, assert and run it successfully using XCUI. 我正在尝试使用xCode 8对视图控制器进行单元测试。我能够使用XCUI记录,声明并成功运行它。 But I want to test particular view controller independently. 但是我想独立测试特定的视图控制器。

Like there are 4 controllers A,B,C and D in sequence (A->B->C->D). 就像依次有四个控制器A,B,C和D(A-> B-> C-> D)。 I want to directly launch C controller and test it independently. 我想直接启动C控制器并对其进行独立测试。

I'm able to test in this way ie default XCUIApplication will open A controller then click on something you will go to B Controller and then click on button in B will take you to C Controller. 我能够以这种方式进行测试,即默认XCUIApplication将打开A控制器,然后单击您将转到B Controller的内容,然后单击B中的按钮将您带到C Controller。 Then you can test it out. 然后,您可以对其进行测试。 But I don't want in sequence. 但是我不想按顺序。 I want to directly test C controller. 我想直接测试C控制器。

Can you please help if anyone else has done same kind of job. 如果其他人也做过同样的工作,可以请您帮忙。 Thanks in advance. 提前致谢。

In XCUITest with Xcode 在使用Xcode的XCUITest中

If you using UITestCase now, you can't directly open D controller 如果现在使用UITestCase,则无法直接打开D控制器

everytime should do sequence (A->B->C->D) because all test case spec cause app launching espectially SignIn, SignOut should be needed for UITest every time 每次都应该执行顺序(A-> B-> C-> D),因为所有测试用例规范都会导致应用特别启动SignIn,因此每次UITest都需要SignOut

supplementary way to use XCUI Test for testCase XCUI测试用于testCase的补充方法

provide A, B, C's sequence function as protocol for minimizing duplicated code 提供A,B,C的序列功能作为协议以最大程度地减少重复代码

ex) define Login protocol 例如)定义登录协议

protocol Loginable {
    var app: XCUIApplication! { get }
    func login()
}

extension Loginable {
    func login() {
        //do login ... typting textfield , tap login button
    }
}

extend your A,B,C,D UITestCase using Loginable to remove duplicated code (because every D uitest case should be logined in example) 使用Loginable扩展您的A,B,C,D UITestCase,以删除重复的代码(因为在示例中应该登录每个D例)

make extension for sequence function to reuse every case 扩展序列函数以重用每种情况

alternative solution is 替代解决方案是

use KIF Functional Test framework 使用KIF功能测试框架

KIF can test UITest based on XCTest instead XCUITest KIF可以基于XCTest而不是XCUITest测试UITest

KIF can access your class like unit test that means KIF可以像单元测试一样访问您的课程,这意味着

you can show your D ViewController to window (set rootViewController directly) and test KIF UItest function 您可以将D ViewController显示到窗口(直接设置rootViewController)并测试KIF UItest函数

ex) KIF 例如)KIF

import XCTest

@testable import YourApp
class DViewControllerTests: XCTestCase {

    var sut: DViewController!
    var window: UIWindow!

    override func setUp() {
        super.setUp()

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        window = appDelegate.window

        sut = //Initialize your D ViewController
        window.rootViewController = sut
        RunLoop.current.run(until: Date()) // cause viewDidLoad
    }

    func testSomeCase() {
        tester().tapView(withAccessibilityIdentifier: "someIdentifier")
        ...
    }
}

KIF can directly launch D controller setting rootViewController to window KIF可以直接启动D控制器,将rootViewController设置为window

Since XCUI tests are integration tests, you have to follow the flow (A->B->C->D) . 由于XCUI测试是集成测试,因此必须遵循流程(A->B->C->D) You can instead do the necessary steps (eg logging in) and then create deep links for your app. 您可以改为执行必要的步骤(例如,登录),然后为您的应用创建深层链接。 Deep linking is a technique that allows an app to be opened to a specific UI or resource, in response to some external event. 深度链接是一种技术,它允许应用程序响应某些外部事件而打开到特定的UI或资源。 As a result you don't have to repeat the sequence of (A->B->C->D) and instead can implement a deep link that directly takes you to D. This way the particular test only checks for the functionality of D view controller. 结果,您不必重复(A->B->C->D)的顺序,而是可以实现直接将您带到D的深层链接。通过这种方式,特定测试仅检查的功能。 D视图控制器。 Following are some useful links: 以下是一些有用的链接:

urbanairship kamyacademy medium.com urbanairship kamyacademy medium.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM