Bre*_*iot 48 xcode unit-testing asynchronous xctest swift
我正在Xcode 6中编写集成测试,以便与我的单元和功能测试一起使用.XCTest有一个setUp()方法,在每次测试之前都会调用它.大!
它还有XCTestException,它让我编写异步测试.也很棒!
但是,我希望在每次测试之前用测试数据填充我的测试数据库,并且setUp只是在异步数据库调用完成之前开始执行测试.
有没有办法让setUp在运行测试之前等待我的数据库准备就绪?
这是我现在所做的一个例子.由于setUp在数据库完成填充之前返回,我必须在每次测试时复制大量测试代码:
func test_checkSomethingExists() {
let expectation = expectationWithDescription("")
var expected:DatabaseItem
// Fill out a database with data.
var data = getData()
overwriteDatabase(data, {
// Database populated.
// Do test... in this pseudocode I just check something...
db.retrieveDatabaseItem({ expected in
XCTAssertNotNil(expected)
expectation.fulfill()
})
})
waitForExpectationsWithTimeout(5.0) { (error) in
if error != nil {
XCTFail(error.localizedDescription)
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我想要的:
class MyTestCase: XCTestCase {
override func setUp() {
super.setUp()
// Fill out a database with data. I can make this call do anything, here
// it returns a block.
var data = getData()
db.overwriteDatabase(data, onDone: () -> () {
// When database done, do something that causes setUp to end
// and start running tests
})
}
func test_checkSomethingExists() {
let expectation = expectationWithDescription("")
var expected:DatabaseItem
// Do test... in this pseudocode I just check something...
db.retrieveDatabaseItem({ expected in
XCTAssertNotNil(expected)
expectation.fulfill()
})
waitForExpectationsWithTimeout(5.0) { (error) in
if error != nil {
XCTFail(error.localizedDescription)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Rnd*_*Tsk 96
您可以waitForExpectationsWithTimeout:handler:
使用在异步测试用例中使用的相同功能,而不是使用信号量或阻塞循环.
// Swift
override func setUp() {
super.setUp()
let exp = expectation(description: "\(#function)\(#line)")
// Issue an async request
let data = getData()
db.overwriteDatabase(data) {
// do some stuff
exp.fulfill()
}
// Wait for the async request to complete
waitForExpectations(timeout: 40, handler: nil)
}
// Objective-C
- (void)setUp {
[super setUp];
NSString *description = [NSString stringWithFormat:@"%s%d", __FUNCTION__, __LINE__];
XCTestExpectation *exp = [self expectationWithDescription:description];
// Issue an async request
NSData *data = [self getData];
[db overwriteDatabaseData: data block: ^(){
[exp fulfill];
}];
// Wait for the async request to complete
[self waitForExpectationsWithTimeout:40 handler: nil];
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*Rob 21
有两种运行异步测试的技术.XCTestExpectation
和信号量.在执行异步操作的情况下setUp
,您应该使用信号量技术:
override func setUp() {
super.setUp()
// Fill out a database with data. I can make this call do anything, here
// it returns a block.
let data = getData()
let semaphore = DispatchSemaphore(value: 0)
db.overwriteDatabase(data) {
// do some stuff
semaphore.signal()
}
semaphore.wait()
}
Run Code Online (Sandbox Code Playgroud)
注意,为了工作,这个onDone
块不能在主线程上运行(否则你将死锁).
如果此onDone
块在主队列上运行,则可以使用run loop:
override func setUp() {
super.setUp()
var finished = false
// Fill out a database with data. I can make this call do anything, here
// it returns a block.
let data = getData()
db.overwriteDatabase(data) {
// do some stuff
finished = true
}
while !finished {
RunLoop.current.run(mode: .default, before: Date.distantFuture)
}
}
Run Code Online (Sandbox Code Playgroud)
这是一种非常低效的模式,但根据overwriteDatabase
实施方式,可能是必要的
注意,如果您知道onDone
块在主线程上运行,则仅使用此模式(否则您将不得不对finished
变量进行一些同步).
小智 7
斯威夫特 4.2
使用这个扩展:
import XCTest
extension XCTestCase {
func wait(interval: TimeInterval = 0.1 , completion: @escaping (() -> Void)) {
let exp = expectation(description: "")
DispatchQueue.main.asyncAfter(deadline: .now() + interval) {
completion()
exp.fulfill()
}
waitForExpectations(timeout: interval + 0.1) // add 0.1 for sure `asyncAfter` called
}
}
Run Code Online (Sandbox Code Playgroud)
和这样的用法:
func testShouldDeleteSection() {
let tableView = TableViewSpy()
sut.tableView = tableView
sut.sectionDidDelete(at: 0)
wait {
XCTAssert(tableView.isReloadDataCalled, "Check reload table view after section delete")
}
}
Run Code Online (Sandbox Code Playgroud)
上面的例子并不完整,但你可以明白这个想法。希望这可以帮助。
归档时间: |
|
查看次数: |
21470 次 |
最近记录: |