[Swift通天遁地]七、数据与安全-(13)单元测试的各个状态和应用

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10340475.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

目录:[Swift]通天遁地Swift

本文将演示单元测试的各个状态和应用。如果项目中没有引用单元格测试框架,

项目导航区点击选中项目名称【DemoApp】,再点击中间列的【+】图标进行添加。

在弹出的模板窗口中,选择单元测试框架模板【iOS Unit Testing Bundle】

->【Next】->保持默认的选项设置->【Finish】

点击刚创建的文件夹【UnitTestProject_DemoTests】左侧的下拉箭头,

显示文件夹下的所有项目。

打开单元测试用例文件【UnitTestProject_DemoTests.Swift】

 1 import XCTest
 2 
 3 class UnitTestProject_DemoTests: XCTestCase {
 4     //配置方法
 5     override func setUp() {
 6         super.setUp()
 7         // Put setup code here. This method is called before the invocation of each test method in the class.
 8         //首先编写单元测试的配置方法。
 9         //配置方法是在测试用例方法运行之前被调用的,
10         //可以在此方法中,进行一些初始化之类的预操作。
11         print("setUp()")
12     }
13     
14     //清理方法
15     override func tearDown() {
16         // Put teardown code here. This method is called after the invocation of each test method in the class.
17         //本方法是在示例代码运行完成之后被调用,
18         //可以在此方法中进行一些清理操作,
19         //比如关闭网络请求的连接等。
20         super.tearDown()
21         print("tearDown()")
22     }
23     
24     //测试用例方法,点击方法左侧的菱形图标,执行该测试用例。
25     func testExample() {
26         // This is an example of a functional test case.
27         // Use XCTAssert and related functions to verify your tests produce the correct results.
28         //在测试用例方法中,输入需要进行测试的代码,
29         //首先创建一个身份证号码,在此进行身份证格式的验证。
30         let peopleID = "350211198203150012"
31         //获得字符串的字符数量。
32         let count = peopleID.count
33         //通过断言,判断身份证号码是否为15位,或18位的长度,
34         //如果判断失败,则输出错误日志。
35         XCTAssert(count == 15 || count == 18, "Incorrect ID number.");
36         //点击方法左侧的菱形图标,执行该测试用例。
37     }
38     
39     func testPerformanceExample() {
40         // This is an example of a performance test case.
41         self.measure {
42             // Put the code you want to measure the time of here.
43         }
44     }    
45 }
原文地址:https://www.cnblogs.com/strengthen/p/10340475.html