新建
工程名
- File
- New
- Target
- iOS Unit Testing Bundle
新版Xcode默认存在
桥接
如果主项目有桥接头文件,单元测试也需要。
Targets
- 主项目target
- 搜索bridging
- 复制桥接头文件内容
- 去新建的测试target中找到相应桥接选项复制进去
更新Podfile
复制原Podfile内容,把原target名改成测试target名
使用pod update --no-repo-update
,不更新pod自身库
示例
import XCTest
@testable import StructDemo_swift
class StructDemo_swiftTests: XCTestCase {
var areaTableVC: ViewController!
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
//获取对应storyboard
let sb = UIStoryboard(name: "Main", bundle: Bundle.main)
//获取对应vc 注意相应vc要在对应storyboard中设置ID 并传入ID
areaTableVC = sb.instantiateViewController(withIdentifier: "AreaViewController") as? ViewController
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testSearchFilter(){
areaTableVC.viewDidLoad()
areaTableVC.searchFilter(text: "test")
print("数目",areaTableVC.searchResults.count ,"总数" ,areaTableVC.areas.count)
XCTAssert(areaTableVC.searchResults.count == 1)
}
}
网友评论