Skip to content

Instantly share code, notes, and snippets.

@barbaramartina
Created May 26, 2016 10:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barbaramartina/2e3613ce3fc2cb95ea79fb8fa0121c6f to your computer and use it in GitHub Desktop.
Save barbaramartina/2e3613ce3fc2cb95ea79fb8fa0121c6f to your computer and use it in GitHub Desktop.
Swift: Unit testing templates
// see: https://www.youtube.com/watch?v=Y3t8e_rUyTk&list=PLwu90VQm-yTobHYmgVdvZu5sBdyPPxW9S and http://ladyandtech.blogspot.de/2016/03/swift-testing-with-xctest.html
import XCTest
@testable import YOUR_MODULE_NAME
class yourTestCase: XCTestCase {
override func setUp() {
super.setUp()
//setup your tests shared variables here
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testSimple() {
// DO something
XCTAssert("something")
}
func testPerformance() {
self.measureBlock {
// DO something to be measured here
}
}
func testAsyncSingleExpectation() {
let expectation = expectationWithDescription("A description")
// DO something async here, like adding an operation to a queue to be executed
anOperation.completionBlock = {
// fulfill your expectation once successfully completed
expectation.fulfill()
}
aQueue1.addOperation(anOperation)
// Wait for the expectation here
waitForExpectationsWithTimeout(10.0) { error in
if let e = error {
XCTFail("Operation did not finish \(e)")
} else {
XCTAssertTrue(true)
}
}
}
func testAsyncMultipleExpectations() {
let expectation1 = expectationWithDescription("A description 1")
let expectation2 = expectationWithDescription("A description 2")
// DO something async here, like adding an operation to a queue to be executed
anOperation1.completionBlock = {
// fulfill your expectation once successfully completed
expectation1.fulfill()
}
aQueue1.addOperation(anOperation1)
// DO something async here, like adding an operation to a queue to be executed.
anOperation2.completionBlock = {
// fulfill your expectation once successfully completed
expectation2.fulfill()
}
aQueue1.addOperation(anOperation2)
// Wait for the expectation here
waitForExpectationsWithTimeout(10.0) { error in
if let e = error {
XCTFail("Operation did not finish \(e)")
} else {
XCTAssertTrue(true)
}
}
}
func testNotification() {
expectationForNotification(UIDeviceBatteryLevelDidChangeNotification, object: UIDevice.currentDevice()) { (NSNotification) -> Bool in
//TODO: do something to check if the notifications brings i.e the information you need OR DO NOT USE A CALLBACK AT ALL, and the result will be true as ong as the notification is raised.
let isNotificationAsExpected = true
return isNotificationAsExpected
}
//DO something to trigger UIDeviceBatteryLevelDidChangeNotification
waitForExpectationsWithTimeout(10.0) { error in
// Make your assertions
}
}
func testExpectationForPredicate() {
// Make sure you have an array as a var in your test case class, names 'names'
// var names: [String] = ["John"]
let testPredicate = NSPredicate(format:"names CONTAINS \"George\"")
expectationForPredicate(testPredicate, evaluatedWithObject: self, handler: nil)
names = ["George"]
waitForExpectationsWithTimeout(10) { error in
// Make your assertions
}
}
func testKVOExpectation() {
// Make sure you have the following vars in your test case class:
// var asyncOperation1: MyAsynchronousOperation? // your operation
// var queue1: NSOperationQueue?
keyValueObservingExpectationForObject(asyncOperation1!, keyPath: "isExecuting", expectedValue: true)
queue1!.addOperation(asyncOperation1!)
waitForExpectationsWithTimeout(10.0) { error in
// TODO: make your assertions
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment