swift - iOS system comes with the unit test study notes

background

Process development for iOS IM clients often need to test business logic inside the SDK, such as protocol header is correct, you need to communicate with the server debug it. This time if you write a page, click on the button calls through, it seemed more superfluous. So I think of swift how to use unit testing, hereby record.

MacOS: 10.15.3
Xcode: 11.3.1
Last updated: 2020-03-16

step

Create a unit test project

file->new->Target...
Select Unit Testing Bundle
Unit test project name, the default can be directly
Direct default, finish can be.

Created

Create a unit test files

In the newly created unit test project, right-click.
New File...
Select the Unit Test Case Class
After entering the test file name + Tests, directly next to the next step
Unit Testing file name
Created

Write test code

First, the introduction of the module to be tested (project name):

@testable import CoffeeChat // @testable 可访问 internal 属性方法

Write test method (similar to the Main function entry):

// 格式为 test+名字,否则无法运行,如下
func testConnect(){
    let client = CIMClient()
    client.connect(ip: "10.0.106.117", port:8000)
    sleep(3)
}

The complete code is as follows:

//
//  CIMClientTests.swift
//  CoffchatTests
//
//  Created by xuyingchun on 2020/3/16.
//  Copyright © 2020 Xuyingchun Inc. All rights reserved.
//

import XCTest

@testable import Coffchat

class CIMClientTests: XCTestCase {
    func testConnect(){
        let client = CIMClient()
        client.connect(ip: "10.0.106.117", port:8000)
        
        sleep(3)
    }
}

Test execution

To test the function click on the line number position "Run" icon.
Examples
Of course, also in the following ways:
shortcut command + u
hot key

tips
unit tests can be divided into modules, can be divided into small function, you happy enough

This is used in the system comes with the unit testing, there are other programs online

Test results and list:
Test unit list

Delete unit test project

  1. Delete unit test folder, such as "CoffchatTests"
  2. Delete target, as follows (only if you delete a folder, without deleting the target will complain)
    1. Click on the project name
    2. Click the "left side to expand"
    3. Right-click delete on target to be deleted
      How to delete a unit test target

Remark

Test Method Unit common

XCTAssert(expression, format...)当expression求值为TRUE时通过

Other Reference

XCTFail(format…) 生成一个失败的测试;
XCTAssertNil(a1, format...)为空判断,a1为空时通过,反之不通过;
XCTAssertNotNil(a1, format…)不为空判断,a1不为空时通过,反之不通过;
XCTAssert(expression, format...)当expression求值为TRUE时通过;
XCTAssertTrue(expression, format...)当expression求值为TRUE时通过;
XCTAssertFalse(expression, format...)当expression求值为False时通过;
XCTAssertEqualObjects(a1, a2, format...)判断相等,[a1 isEqual:a2]值为TRUE时通过,其中一个不为空时,不通过;
XCTAssertNotEqualObjects(a1, a2, format...)判断不等,[a1 isEqual:a2]值为False时通过;
XCTAssertEqual(a1, a2, format...)判断相等(当a1和a2是 C语言标量、结构体或联合体时使用, 判断的是变量的地址,如果地址相同则返回TRUE,否则返回NO);
XCTAssertNotEqual(a1, a2, format...)判断不等(当a1和a2是 C语言标量、结构体或联合体时使用);
XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...)判断相等,(double或float类型)提供一个误差范围,当在误差范围(+/-accuracy)以内相等时通过测试;
XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...) 判断不等,(double或float类型)提供一个误差范围,当在误差范围以内不等时通过测试;
XCTAssertThrows(expression, format...)异常测试,当expression发生异常时通过;反之不通过;(很变态) XCTAssertThrowsSpecific(expression, specificException, format...) 异常测试,当expression发生specificException异常时通过;反之发生其他异常或不发生异常均不通过;
XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...)异常测试,当expression发生具体异常、具体异常名称的异常时通过测试,反之不通过;
XCTAssertNoThrow(expression, format…)异常测试,当expression没有发生异常时通过测试;
XCTAssertNoThrowSpecific(expression, specificException, format...)异常测试,当expression没有发生具体异常、具体异常名称的异常时通过测试,反之不通过;
XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...)异常测试,当expression没有发生具体异常、具体异常名称的异常时通过测试,反之不通过

Reference: https://www.jianshu.com/p/be32739d8aae

Published 19 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/xmcy001122/article/details/104897575