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

参考文档iOS Unit Testing and UI Testing Tutorial | Kodeco, the new raywenderlich.com

测试案例

创建一个单元测试

    func testExample() throws {
        let personID:String = "0123456789"
        let count = personID.count
        XCTAssert(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 Foundation

class EmailUtil {
    func validateEmail(email:String) -&gt; 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 Foundation

struct 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 XCTest



final 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

原文地址:https://blog.csdn.net/linzhiji/article/details/128116714

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_36768.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注