1、XCTest框架概述
XCTest是苹果官方的测试框架,是基于OCUnit的传统测试框架,测试编写起来非常简朴。
XCTest 的优缺点:
1)优点:与 Xcode 深度集成,有专门的Test 导航栏,
2)缺点:由于受限于官方测试API,因此功能不是很丰富。在誊写性和可读性上都不太好。在测试用例太多的时间,由于各个测试方法是割裂的,想在某个很长的测试文件中找到特定的某个测试并搞明确这个测试是在做什么并不是很轻易的事变。全部的测试都是由断言完成的,而许多时间断言的意义并不是特别的明确,对于项目交付大概新的开发职员加入时,通常要花上很大成原来举行明确大概转换。别的,每一个测试的形貌都被写在断言之后,混淆在代码之中,难以探求。利用XCTest测试别的一个标题是难以举行mock大概stub。
2、XCTestCase概述
XCTestCase是苹果官方提供的一个单元测试工具,它的初始化不是用户控制的,开发者无需手动针对XCTestCase的subclass举行alloc和init大概调用静态方法初始化的操纵。
针对一个功能模块的单元测试(针对某个class),只必要单独给这个类创建一个继续于XCTestCase,在文件中实现下面根本函数后(一样寻常体系会默认创建这三个函数),必要测试的逻辑只必要开发者自行界说以test开头的函数,然后在那边实现本身针对某个函数、返回数值效果、操纵等的测试脚本即可,按comman+u实验,函数头上出现出现蓝色的标记体现通过测试,否则直接报赤色错误。
import XCTest@testable import test class testTests: XCTestCase { override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. } override func tearDown() { // Put teardown code here. This method is called after the invocation of each test method in the class. super.tearDown() } func testExample() { // This is an example of a functional test case. // Use XCTAssert and related functions to verify your tests produce the correct results. } func testPerformanceExample() { // This is an example of a performance test case. self.measure { // Put the code you want to measure the time of here. } } }从表明我们可以知道这4个函数的意思
函数用途setUp继续与XCTestCase 函数测试文件开始实验的时间运行tearDown继续与XCTestCase 测试函数运行完之后实验testExample测试的例子函数testPerformanceExample性能测试3、利用XCTest框架举行单元测试
1)创建一个单元测试Target
单元测试target的创建方式有2种
方式一:在创建新项目时,勾选 Include Unit Tests,项目创建完成绩会生成一个单元测试target,target名称默以为 项目名称+Tests
方式二:在已存在的项目中创建,按comman+5 打开xcode的测试导航器,点击左下角的 + 按钮,然后从菜单中选择 New Unit Test Target…
函数阐明testExample全局变量f1 + f2 相加是否即是固定的数,断言是否相当testIsPrimenumber判断是否是素数 断言是否返回真import XCTest@testable import test class SampleTests: XCTestCase { var f1 : Float? var f2 : Float? override func setUp() { super.setUp() //在测试方法实验前设置变量 f1 = 10.0 f2 = 20.0 } override func tearDown() { //在测试方法实验完成后,扫除变量 super.tearDown() } func testExample() { XCTAssertTrue(f1! + f2! == 30.0) } //simpleTest func testIsPrimenumber(){ let oddNumber = 5 XCTAssertTrue(isPrimenumber(Double(oddNumber))) } func isPrimenumber(_ number : Double)->Bool{ for no in 1...Int(sqrt(number)) { if Int(number)/no != 0{ return true } } return false } func testPerformanceExample() { // This is an example of a performance test case. self.measure { // Put the code you want to measure the time of here. } } }
XCTAssert测试模子
创建一个BullsEyeGame模子类
import Foundation class BullsEyeGame { var round = 0 let startValue = 50 var targetValue = 50 var scoreRound = 0 var scoreTotal = 0 init() { startNewGame() } func startNewGame() { round = 0 scoreTotal = 0 startNewRound() } func startNewRound() { round = round + 1 scoreRound = 0 targetValue = 1 + (Int(arc4random()) % 100) } func check(guess: Int) -> Int { let difference = abs(targetValue - guess) // let difference = guess - targetValue scoreRound = 100 - difference scoreTotal = scoreTotal + scoreRound return difference }}用XCTAssert测试BullsEyeGame模子类中一个核心功能:一个BullsEyeGame对象可以或许正确盘算出一局游戏的得分吗?
告急步调:
1.在BullsEyeTests.swift中,import语句下面添加 :@testable import test