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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-vusnrqsn-mb.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

目录:[Swift]通天遁地Swiftgit

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

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

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

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

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

显示文件夹下的全部项目。ide

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

 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 }
相关文章
相关标签/搜索