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

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

Swift的KIF设置

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

为了触发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测试了。

Swift中KIF测试的特点-b的更多相关文章

  1. Swift: 比较Swift中闭包传值、OC中的Block传值

    一.介绍 开发者对匿名函数应该很清楚,其实它就是一个没有名字的函数或者方法,给人直观的感觉就是只能看到参数和返回值.在iOS开发中中,它又有自己的称呼,在OC中叫Block代码块,在Swift中叫闭包 ...

  2. swift中的结构体和枚举

    Swift 里的结构体非常特殊. 类是面向对象编程语言中传统的结构单元.和结构体相比,Swift 的类支持实现继承,(受限的)反射,析构函数和多所有者. 既然类比结构体强大这么多,为什么还要使用结构体 ...

  3. 在Swift中应用Grand Central Dispatch(下)

    在第一部分中, 你学到了并发,线程以及GCD的工作原理.通过使用dispatch_barrrier和dispatch_sync,你做到了让 PhotoManager单例在读写照片时是线程安全的.除此之 ...

  4. Swift 中范围和区间如何使用?

    虽然现在swift语言已经发展到了2.0版了,但是相信很多学习iOS开发的童鞋仍对swift语言存在各种各样的疑问,今天小编将为大家详细介绍swift中的范围和区间,下面我们一起来看看吧. Range ...

  5. Swift中使用构建配置来支持条件编译-b

    在Objective-C中,我们经常使用预处理指令来帮助我们根据不同的平台执行不同的代码,以让我们的代码支持不同的平台,如: 1 2 3 4 5 6 7 8 9 #if TARGET_OS_IPHON ...

  6. 在Swift中使用遗留的C API

    Swift的类型系统的设计目的在于简化我们的生活,为此它强制用户遵守严格的代码规范来达到这一点.毫无疑问这是一件大好事,它鼓励程序员们编写 更好更正确的代码.然而,当Swift与历史遗留的代码库.特别 ...

  7. Swift中的设计模式

    设计模式(Design Pattern)是 对软件设计中普遍存在的各种问题,所提出的解决方案.这个术语是由埃里希·伽玛等人(Erich Gamma,Richard Helm,Ralph Johnson ...

  8. 如何在 Swift 中优雅地处理 JSON

    阅读目录 在Swift中使用JSON的问题 开始 基础用法 枚举(Enumeration) 下标(Subscripts) 打印 调试与错误处理 后记   因为Swift对于类型有非常严格的控制,它在处 ...

  9. Swift 中 String 取下标及性能问题

    Swift 中 String 取下标及性能问题 取下标 String String 用 String.Index 取下标(subscript)得到 Character,String.Index 要从 ...

随机推荐

  1. AngularJS的开发工具---yeoman 简易安装

    AngularJS 不错,yeoman作为推荐开发工具,网上的安装步骤较烦,这里给出简易步骤. 1.安装 Ruby     自己到 Ruby 官方下载最新安装包: http://rubyinstall ...

  2. cocos2d-x3.0 ListView

    .h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...

  3. gprof + kprof + gprof2dot (性能 与 函数调用图)-

    http://www.cnblogs.com/rocketfan/archive/2009/11/15/1603465.html http://blog.csdn.net/stanjiang2010/ ...

  4. linux下sqlite3可视化工具

    1.介绍:sqlite3是linux上的小巧的数据库,一个文件就是一个数据库. 2.安装:要安装sqlite3,可以在终端提示符后运行下列命令:sudo apt-get install sqlite3 ...

  5. navigation的pushViewController卡顿问题

    问题:在ios中一个viewController中,添加下面代码: <pre name="code" class="objc">UIViewCont ...

  6. wpf提示背景,资源样式

    查找资源时多用element.TryFindResource() <TextBox FontSize="17" Height="26" Margin=&q ...

  7. Linux下长时间ping网络加时间戳并记录到文本(转)

    [root@test ~]# ping 192.168.2.1 -c 10 PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.64 bytes f ...

  8. 完全卸载oracle

    今天在网上看到有位网友写的篇日志,感觉蛮好的,一般卸载oracle有4个地方需求注意:1)Services,2)software,3eventlog,4)path. 1.关闭 oracle 所有的服务 ...

  9. SQL语句之三简单增删改查

    这是前面建的库和表 USE Test go INSERT dbo.MyTable --插入数据         ( NAME ,age) VALUES  ( '数据,20  -- NAME - var ...

  10. ktv

    自制KTV点歌系统经验 Windows Media Player控件播放       Windows Media Player控件的简单使用 1.播放一首歌曲的方法 Windows Media Pla ...