参考博客:
iOS单元测试概念了解
iOS异步测试
Xcode:为你的项目集成单元测试(unit tests)时记得避开这些坑
1.创建测试工程
File-->New--->Target-->iOS Unit Testing Bundle
image.png2.引用主工程
@testable import 主工程名
image.png
3.podfile中引入测试工程
target 'YStarTests' do
inherit! :search_paths
end
image.png
4.项目配置文件调整
-
Enable Testability
image.png -
info plist
image.png
5.单元测试编写
image.png- 逻辑测试
//逻辑测试举例: 测试期望结果与实际结果是否相等
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.执行单元测试
image.png- 逻辑测试结果
- 功能测试结果
- 性能测试结果
网友评论