xcode swift 单元测试 test
XCTest是苹果官方的测试框架,是基于OCUnit的传统测试框架,测试编写起来非常简单。
参考文档:iOS Unit Testing and UI Testing Tutorial | Kodeco, the new raywenderlich.com
测试案例一
创建一个单元测试
func testExample() throws {let personID:String = "0123456789"let count = personID.countXCTAssert(count <= 10, "ID length error.")// This is an example of a functional test case.// Use XCTAssert and related functions to verify your tests produce the correct results.// Any test you write for XCTest can be annotated as throws and async.// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.}
常用的一些断言
XCTAssertEqualObjects:当两个对象不相等或者是其中一个对象为nil时,断言失败。
XCTAssertEquals:当参数1不等于参数2时断言失败,用于基本数据测试。
XCTAssertNil:当参数不是nil时,断言失败。
XCTAssertNotNil:当参数是nil时,断言失败。
XCTAssertTrue:当表达式为false时,断言失败。
XCTAssertFalse:当表达式为true时,断言失败。
XCTAssertThrows:如果表达式没有抛出异常,则断言失败。
XCTAssertNoThrow:如果表达式抛出异常,则断言失败
测试案例二
EmailUtil.swift
import Foundationclass EmailUtil {func validateEmail(email:String) -> Bool {//这里传入的参数,需要补充一下关于正则表达式的一些相关知识,学习链接放在文末。let pattern = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"let matcher = RegexHelper(pattern: pattern)let result = matcher.match(input: email)if result {return true}else{return false}}
}
RegexHelper.swift
import Foundationstruct RegexHelper {let regex : NSRegularExpression?init(pattern:String) {do {regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)} catch {regex = nil}}func match(input:String) -> Bool {let matches = regex?.matches(in: input, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, input.lengthOfBytes(using: String.Encoding.utf8)))if (matches != nil) {return matches!.count > 0}else{return false}}
}
创建测试案例 EmailTester
import XCTestfinal class EmailTester: XCTestCase {func testEmail() throws {let emailUtil = EmailUtil()let result = emailUtil.validateEmail(email: "s1@163.")XCTAssert(result,"邮箱格式不正确")}}
会报错:Cannot find xxxx in scope ,改一下Target Membership
测试符合预期
修改邮箱
测试通过
使用XCTest框架进行性能测试
func testPerformanceExample() throws {measure {for _ in 0...600 {let image = UIImage(named: "wind")print(image?.size)}}}
UITesting界面测试
参考
swift 单元测试1 - 简书
iPhone开发Swift基础06 单元测试和界面测试_乐事派的博客-CSDN博客_swift 单元测试
官网
Apple Developer Documentation
xcode swift 单元测试 test相关推荐
- Swift 单元测试
Swift 单元测试参考 什么是单元测试 单元测试: 针对程序模块的最小单元来进行正确性检验的测试工作.对于面向对象编码来说,最小单元就是方法 其实就是验证一些基本规则. 基本原则 命名 使用领域命名 ...
- swift单元测试(三)XCTest之UI测试UITest
继上篇文章介绍了XCTest中的单元测试,这片土地主要介绍下苹果官方测试框架XCTest中的的UITest. 1.UITest简介 UI Tests是一个自动测试UI与交互的Testing组件.UI ...
- Xcode 5 单元测试(一)使用XCTest进行单元测试
在Objc.io #1的Testing View Controllers中讲解的就是单元测试的相关内容.本文说下如何通过Xcode 5中集成的XCTest框架进行简单的单元测试. 什么是单元测试 首先 ...
- Xcode Swift package manager 无法下载 GitHub 包问题
解决方法: 使用终端进入项目根目录 复制并在终端运行终端代理命令 最后运行如下命令 xcodebuild -resolvePackageDependencies -scmProvider system ...
- swift 引用swift_Swift Swift中的单元测试:基础知识
swift 引用swift In this first part of the Unit Testing in Swift series, we will cover how to add a uni ...
- xcode swift_CocoaPods Swift XCode教程
xcode swift As a beginner or seasoned iOS Developer, at some point in time, you must have come acros ...
- iOS 单元测试和UI测试教程
原文:iOS Unit Testing and UI Testing Tutorial 作者:Audrey Tam 译者:kmyhy 编写测试不是为了追求刺激,测试是为了避免你崭新的 App 变成了充 ...
- xcode13 swift语言 ios开发 快捷代码优化方式(代码重构)例子
xcode13 swift语言 ios开发 快捷代码优化方式(代码重构)例子 这是自己在CSDN上发的第二篇文章,开始用markdown编辑,更加美观一些. 问题描述 在用xcode swift语言开 ...
- C++入门到精通(xcode IDE)
文章目录 Xcode swift 区别联系 实例 C++基础编程 编译过程 入门实例 编程实战 linux 下编译c++ 程序=算法+数据结构 C++存储类 数组 变量 常量 关键字 识符命名规则 数 ...
最新文章
- 价值1143元的《R语言统计分析微生物组数据(Statistical Analysis of Microbiome Data with R)》系列图书
- AI同传效果媲美人类,百度翻译出品全球首个上下文感知机器同传模型
- C#的winform拼数字游戏
- svn 合并分支代码到主干
- 关于表达式i+++i+++i++有感
- 交叉编译指定运行时库路径_运行时vs编译时类路径
- 运行时vs编译时类路径
- 批处理创建程序的快捷方式
- gojs 部分功能实现
- mysql1440秒未活动_phpMyAdmin登陆超时1440秒未活动请重新登录
- 将DHCP从win2000转移到2003上
- 宇视摄像头ip搜索软件下载_搜索资源?下载资源?一款软件就给你安排的妥妥的...
- vivo X Fold屏幕规格揭晓:搭载2K/120Hz E5折叠屏幕
- spring cloud 和 dubbo 各自的优缺点
- hihocoder第237周:三等分带权树
- shell脚本练习实例
- Image Style Transfer:多风格 TensorFlow 实现
- Axure RP 8.0安装和汉化
- Windows运维之道——PXE网克系统
- python--循环绘制ERA5风场的空间分布图