Swift中KIF测试的特点-b

我最近在忙着回归到过去测试代码的老路子,使用KIF和XCTest框架,这样会使得iOS中的测试变得简单。当我开始捣鼓KIF的时候,我用Swift写的应用出了点小问题,不过最终还是很机智的搞定了。在我写Swift的时候我还发现了不少Swift独有的模式,这是个令我相当愉快的事,所以我们可以拿来分享分享。

这里我用的示例Demo是自己开发的Seinfeld Quotes应用,现在我只是简单的测试添加条目的功能,下图是用KIF进行的测试:

a.gif

Swift的KIF设置

设置KIF的话只要照着KIF README就好了。成功设置之后,一定要确认你在最外层的Bridging Header头文件里导入了KIF包,如下:

Screen-Shot-2014-10-05-at-10.19.42-AM-1024x587.png

为了触发Bridging Header头文件的生成,我通常会先在目标里用Objective C写一个测试文件,然后当Bridging Header生成之后我就会删除它。

于是你觉得可以开始使用KIF了是吧,不过我想告诉你一个悲哀的事情,调试器(tester)在KIF里面是用#define定义的,而#define在Swift里面是不可用的!

当我按照@bnickel在Github上的Swift KIF Issue里的建议添加了一段扩展之后,KIF就能在Swift下兼容了。这里的修改相当小,而且尽量保证了tester和system语法上的原汁原味:

//
//  KIF+SwiftExtension.swift
//  SeinfeldQuotes
//
//  Created by Natasha Murashev on 10/5/14.
//  Copyright (c) 2014 NatashaTheRobot. All rights reserved.
//
 
extension XCTestCase {
     
    var tester: KIFUITestActor { return tester() }
    var system: KIFSystemTestActor { return system() }
     
    private func tester(_ file : String = __FILE__, _ line : Int = __LINE__) -> KIFUITestActor {
        return KIFUITestActor(inFile: file, atLine: line, delegate: self)
    }
     
    private func system(_ file : String = __FILE__, _ line : Int = __LINE__) -> KIFSystemTestActor {
        return KIFSystemTestActor(inFile: file, atLine: line, delegate: self)
    }
}

Accessibility Labels

KIF使用了Accessibility Labels来抓取任意屏幕上的View组件,因为Accessibility Labels可以轻易改变(比如说按钮标题改变)。而我偏好于用常数来标记每一个测试用的Accessibility Labels。

在Swift中我还发现了可以很好的在private扩展中通过枚举将这些Accessibility Labels放到一起:

// AddQuoteTests.swift
 
class AddQuoteTests: KIFTestCase {
     
}
 
// MARK: Setup Accessibility Labels
 
private extension AddQuoteTests {
    enum ButtonLabel: String {
        case Add = "Add"
        case Save = "Save"
    }
     
    enum NavBarTitle: String {
        case CreateQuote = "Create Quote"
        case ListQuotes = "Seinfeld Quotes"
    }
     
    enum TextFieldLabel: String {
        case QuoteContent = "Quote Content"
        case SceneDescription = "Scene Description"
    }
     
    enum TextInput: String {
        case QuoteContent = "My Awesome Quote"
        case SceneDescription = "My Scene"
    }
     
}

测试

我比较喜欢让自己的代码清晰可读,所以我测试乍看上去相当简单:

//
//  AddQuoteTests.swift
//  SeinfeldQuotes
//
//  Created by Natasha Murashev on 10/5/14.
//  Copyright (c) 2014 NatashaTheRobot. All rights reserved.
//
 
class AddQuoteTests: KIFTestCase {
     
    func testAddQuote() {
        tapAddButton()
        fillInQuoteData()
        saveQuote()
        assertNewQuoteCreated()
    }
     
}

而其实我把更多的细节实现在了一个private的扩展里:

class AddQuoteTests: KIFTestCase {
     
    func testAddQuote() {
        tapAddButton()
        fillInQuoteData()
        saveQuote()
        assertNewQuoteCreated()
    }
     
}
 
// MARK: Test Details
 
private extension AddQuoteTests {
     
    func tapAddButton() {
        tester.tapViewWithAccessibilityLabel(ButtonLabel.Add.toRaw(), traits: UIAccessibilityTraitButton)
        tester.waitForViewWithAccessibilityLabel(NavBarTitle.CreateQuote.toRaw(), traits: UIAccessibilityTraitStaticText)
    }
     
    func fillInQuoteData() {
        tester.enterText(TextInput.QuoteContent.toRaw(), intoViewWithAccessibilityLabel: TextFieldLabel.QuoteContent.toRaw())
        tester.enterText(TextInput.SceneDescription.toRaw(), intoViewWithAccessibilityLabel: TextFieldLabel.SceneDescription.toRaw())
    }
     
    func saveQuote() {
        tester.tapViewWithAccessibilityLabel(ButtonLabel.Save.toRaw(), traits: UIAccessibilityTraitButton)
        tester.waitForViewWithAccessibilityLabel(NavBarTitle.ListQuotes.toRaw(), traits: UIAccessibilityTraitStaticText)
    }
     
    func assertNewQuoteCreated() {
        tester.waitForViewWithAccessibilityLabel(TextInput.QuoteContent.toRaw(), traits: UIAccessibilityTraitStaticText)
        tester.waitForViewWithAccessibilityLabel(TextInput.SceneDescription.toRaw(), traits: UIAccessibilityTraitStaticText)
    }
}

这样就可以在Swift里进行各种各样的KIF测试了。

原文地址:https://www.cnblogs.com/isItOk/p/5599249.html