iOS单元测试

发表于:2017-7-11 13:06

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:木头与琉璃    来源:51Testing软件测试网采编

  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.执行单元测试
 
  逻辑测试结果
 
  功能测试结果
 
  性能测试结果
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号