1.创建测试工程
File-->New--->Target-->iOS Unit Testing Bundle
2.引用主工程
@testable import 主工程名 |
3.podfile中引入测试工程
target 'YStarTests' do inherit! :search_paths end |
4.项目配置文件调整
Enable Testability
info plist
5.单元测试编写
逻辑测试
//逻辑测试举例: 测试期望结果与实际结果是否相等 func testAdd(){ let expectValue = 20 let actualValue = 30 XCTAssertEqual(expectValue, actualValue) } |
//功能测试:测试登录功能(异步测试) func testLogin() { let expectOption = expectation(description: "登录测试") let param = LoginRequestModel() param.phone = "18657195470" param.pwd = "123456".md5() AppAPIHelper.commen().login(model: param, complete: { (result) -> ()? in if let object = result as? StarUserModel{ if let uid = object.userinfo?.id{ ShareModelHelper.instance().uid = Int(uid) UserDefaults.standard.set(uid, forKey: AppConst.UserDefaultKey.uid.rawValue) } if let phone = object.userinfo?.phone{ ShareModelHelper.instance().phone = phone UserDefaults.standard.set(phone, forKey: AppConst.UserDefaultKey.phone.rawValue) } ShareModelHelper.instance().token = object.token UserDefaults.standard.set(object.token, forKey: AppConst.UserDefaultKey.token.rawValue) expectOption.fulfill() } return nil }, error: nil) waitForExpectations(timeout: 15, handler: nil) } |
//性能测试:测试登录功能和Add方法性能表现 func testPerformanceExample() { // This is an example of a performance test case. self.measure { // Put the code you want to measure the time of here. self.testLogin() self.testAdd() } } |
6.执行单元测试
逻辑测试结果
功能测试结果
性能测试结果