Swift单元测试Quick+Nimble

news/2024/7/20 23:13:10 标签: ios, swift

文章目录

    • 使用Quick+Nimble
      • 1、苹果官方测试框架XCTest的优缺点
      • 2、选择Quick+Nimble的原因:
      • 3、Quick+Nimble使用介绍
        • 集成:
        • Quick关键字说明:
        • Nimble中的匹配函数
          • 等值判断:使用equal函数
          • 是否是同一个对象:使用beIdenticalTo函数
          • 比较:
          • 比较浮点数
          • 类型检查
          • 是否为真
          • 是否有异常
          • 集合关系
          • 字符串
          • 检查集合中的所有元素是否符合条件
          • 检查集合个数
          • 匹配任意一种检查
      • 4、Quick使用总结

使用Quick+Nimble

github地址

1、苹果官方测试框架XCTest的优缺点

优点:与 Xcode 深度集成,有专门的Test 导航栏。

缺点:
1)因为受限于官方测试API,因此功能不是很丰富。
2)在书写性和可读性上都不太好。在测试用例太多的时候,由于各个测试方法是割裂的,想在某个很长的测试文件中找到特定的某个测试并搞明白这个测试是在做什么并不是很容易的事情。
3)所有的测试都是由断言完成的,而很多时候断言的意义并不是特别的明确,对于项目交付或者新的开发人员加入时,往往要花上很大成本来进行理解或者转换。另外,每一个测试的描述都被写在断言之后,夹杂在代码之中,难以寻找。
4)使用XCTest测试另外一个问题是难以进行mock或者stub

2、选择Quick+Nimble的原因:

主要是由于苹果官方框架的测试方法及断言不明确,可读性不好,难以分辨,交接项目需要花费的时间很多,所以建议采用三方测试框架
目前主流的三方测试框架主要有:
oc中:kiwi 、specta、cedar
swift:quick+nimble、Sleipnir
由于项目是使用的swift语言,所以主要采用quick+nimble,用于单元测试和断言。
如果你的项目是OC的,推荐使用kiwi,目前是start最多的三方框架。

3、Quick+Nimble使用介绍

Quick 是一个建立在XCTest 上,为Swift 和Objective-C 设计的测试框架. 对测试使用Swift编写的App非常友好,对Swift使用者来说,Quick是最佳选择
它通过DSL 去编写非常类似于RSpec 的测试用例。
Nimble 就像是Quick 的搭档,它提供了匹配器作为断言,用于编写匹配模式。

集成:

使用pod集成方便快捷:

pod ‘Quick’
pod ‘Nimble’

新建一个测试类,继承于QuickSpec父类,然后重写spec( )方法
示例代码:

finalfinal class BindDeviceTests: QuickSpec {

    override func spec() {
        //所有测试放在这里
        
        describe("test BindDeviceDB") {
            let findMac = "34:94:54:C2:E3:C6"
            let bindedMacs = ["34:94:54:C2:E3:C6",
                              "D8:0B:CB:62:08:5F",
                              "FF:F2:00:08:21:9C"]
            
            it("test saveBindDevice") {
                let bindDevice = BindDevice()
                bindDevice.scaleName = "test"
                bindDevice.userId = testLoginUserId
                bindDevice.mac = testMac
                expect(bindDevice.save()).to(beTrue())
            }
            
            it("test findBindDeviceList") {
                let list = BindDevice.findBindDeviceList()
                printLog(message: "test findBindDeviceWithMac: \(String(describing: list?.count))")
                expect(list?.count) == 6
            }
			
            ......
			
            xit("test findNotUploadBindDevices") {
                let list = BindDevice.findNotUploadBindDevices()
                printLog(message: "test findNotUploadBindDevices: \(String(describing: list?.count))")
                expect(list).to(beNil())
            }

        }
    }

}
Quick关键字说明:

在这里插入图片描述

Nimble中的匹配函数
等值判断:使用equal函数
  • expect(actual).to(equal(expected))
  • expect(actual) == expected
  • expect(actual) != expected
是否是同一个对象:使用beIdenticalTo函数
  • expect(actual).to(beIdenticalTo(expected))
  • expect(actual) === expected
  • expect(actual) !== expected
比较:
  • expect(actual).to(beLessThan(expected))
  • expect(actual) < expected
  • expect(actual).to(beLessThanOrEqualTo(expected))
  • expect(actual) <= expected
  • expect(actual).to(beGreaterThan(expected))
  • expect(actual) > expected
  • expect(actual).to(beGreaterThanOrEqualTo(expected))
  • expect(actual) >= expected
比较浮点数
  • expect(10.01).to(beCloseTo(10, within: 0.1))
类型检查
  • expect(instance).to(beAnInstanceOf(aClass))
  • expect(instance).to(beAKindOf(aClass))
是否为真
  • expect(actual).to(beTruthy())
  • expect(actual).to(beTrue())
  • expect(actual).to(beFalsy())
  • expect(actual).to(beFalse())
  • expect(actual).to(beNil())
是否有异常
  • // Passes if actual, when evaluated, raises an exception:
  • expect(actual).to(raiseException())
  • // Passes if actual raises an exception with the given name:
  • expect(actual).to(raiseException(named: name))
  • // Passes if actual raises an exception with the given name and reason:
  • expect(actual).to(raiseException(named: name, reason: reason))
  • // Passes if actual raises an exception and it passes expectations in the block
  • // (in this case, if name begins with ‘a r’)
  • expect { exception.raise() }.to(raiseException { (exception: NSException) in
    • expect(exception.name).to(beginWith(“a r”))
  • })
集合关系
  • // Passes if all of the expected values are members of actual:
  • expect(actual).to(contain(expected…))
  • expect([“whale”, “dolphin”, “starfish”]).to(contain(“dolphin”, “starfish”))
  • // Passes if actual is an empty collection (it contains no elements):
  • expect(actual).to(beEmpty())
字符串
  • // Passes if actual contains substring expected:
  • expect(actual).to(contain(expected))
  • // Passes if actual begins with substring:
  • expect(actual).to(beginWith(expected))
  • // Passes if actual ends with substring:
  • expect(actual).to(endWith(expected))
  • // Passes if actual is an empty string, “”:
  • expect(actual).to(beEmpty())
  • // Passes if actual matches the regular expression defined in expected:
  • expect(actual).to(match(expected))
检查集合中的所有元素是否符合条件
  • // with a custom function:
  • expect([1,2,3,4]).to(allPass({$0 < 5}))
  • // with another matcher:
  • expect([1,2,3,4]).to(allPass(beLessThan(5)))
检查集合个数
  • expect(actual).to(haveCount(expected))
匹配任意一种检查
  • // passes if actual is either less than 10 or greater than 20
  • expect(actual).to(satisfyAnyOf(beLessThan(10), beGreaterThan(20)))
  • // can include any number of matchers – the following will pass
  • expect(6).to(satisfyAnyOf(equal(2), equal(3), equal(4), equal(5), equal(6), equal(7)))
  • // in Swift you also have the option to use the || operator to achieve a similar function
  • expect(82).to(beLessThan(50) || beGreaterThan(80))

4、Quick使用总结

  • 使用Quick,编写it方法执行多个test方法,实际执行顺序,按照字母排序执行,可以从控制台打印得出
  • 单元测试的方法,保存、删除、修改等会对数据库真正意义上的修改
  • 使用xit,表示不测试这些方法
  • 当既有it又有fit,表示只会测试fit的方法( 只要存在f开头的方法,单元测试开始执行便只会执行f开头的方法,即使不在同一个测试类中
  • 当使用describe、context、it嵌套使用时,当最外层方法使用了x开头的,整个入口都不会进入测试,即使嵌套里面使用了f开头的

http://www.niftyadmin.cn/n/5313964.html

相关文章

HDU - 2063 过山车(Java JS Python C)

题目来源 Problem - 2063 (hdu.edu.cn) 题目描述 RPG girls今天和大家一起去游乐场玩&#xff0c;终于可以坐上梦寐以求的过山车了。 可是&#xff0c;过山车的每一排只有两个座位&#xff0c;而且还有条不成文的规矩&#xff0c;就是每个女生必须找个男生做partner和她同坐…

经典算法-遗传算法的python实现

经典算法-遗传算法的python实现 前言 本文分享经典的算法&#xff1a;遗传算法受到生物进化理论启发&#xff0c;模拟生物种群的进化过程。遗传算法是一类基于生物进化理论的优化算法&#xff0c;通过模拟生物进化的过程&#xff0c;通过选择、交叉和变异等操作&#xff0c;不…

SQLAlchemy 中的会话(Session)缓存详解

SQLAlchemy 的会话缓存&#xff08;Session Cache&#xff09;是 ORM 框架的核心特性之一&#xff0c;对于理解和高效使用 SQLAlchemy 至关重要。这个缓存机制主要作用在会话&#xff08;Session&#xff09;层面&#xff0c;提供了对数据库交互的中间缓存层。以下是对 SQLAlch…

xcode-docC

docC(documentaion compiler) docC分为三种类型&#xff1a;articles,tutorials articles 文章介绍类型 tutorials 教程 讲解api的使用流程 苹果官方视频

【开源】基于JAVA+Vue+SpringBoot的大学计算机课程管理平台

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 实验课程档案模块2.2 实验资源模块2.3 学生实验模块 三、系统设计3.1 用例设计3.2 数据库设计3.2.1 实验课程档案表3.2.2 实验资源表3.2.3 学生实验表 四、系统展示五、核心代码5.1 一键生成实验5.2 提交实验5.3 批阅实…

Zabbix相关问题及答案(2024)

1、Zabbix是什么&#xff1f;有什么作用&#xff1f; Zabbix是一个企业级的开源监控解决方案&#xff0c;设计用于监控和跟踪服务器、网络设备、硬件和软件的状态和性能。它由Alexei Vladishev开发&#xff0c;并且被广泛地用于各种不同规模的IT环境管理。 Zabbix的主要作用和…

SQL Server 加密 view文本

CREATE VIEW dbo.View_building WITH ENCRYPTION AS SELECT * FROM Building_Temp; GO 注意&#xff1a; 加密後就看不到VIEW文本了&#xff0c;修改 ALTER VIEW dbo.View_building WITH ENCRYPTION AS –修改後的VIEW 文本 GO 或者刪除再新增。 所以&#xff0c;要另備份原V…

数据结构-函数题

6-2.求二叉树的高度 本题要求给定二叉树的高度。 函数接口定义&#xff1a; int GetHeight( BinTree BT ); typedef struct TNode *Position; typedef Position BinTree; struct TNode{ElementType Data;BinTree Left;BinTree Right; }; 要求函数返回给定二叉树BT的高度值…