xcode swift 单元测试 test
创始人
2024-03-05 10:48:48
0

XCTest是苹果官方的测试框架,是基于OCUnit的传统测试框架,测试编写起来非常简单。

测试案例一

创建一个单元测试

    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

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...