Skip to content

Instantly share code, notes, and snippets.

@BrandonShega
Created December 14, 2016 21:10
Show Gist options
  • Save BrandonShega/dfe3f51107efab54f3b011486b82a4f2 to your computer and use it in GitHub Desktop.
Save BrandonShega/dfe3f51107efab54f3b011486b82a4f2 to your computer and use it in GitHub Desktop.
Playground TDD
import XCTest
class PlaygroundTestObserver: NSObject, XCTestObservation {
@objc func testCase(_ testCase: XCTestCase, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: UInt) {
print("Test failed on line \(lineNumber): \(testCase.name), \(description)")
}
}
struct TestRunner {
func runTests(testClass: AnyClass) {
print("Running test suite \(testClass)")
let tests = testClass as! XCTestCase.Type
let testSuite = tests.defaultTestSuite()
testSuite.run()
let run = testSuite.testRun as! XCTestSuiteRun
print("Ran \(run.executionCount) tests in \(run.testDuration) with \(run.totalFailureCount) failures")
}
}
class ExampleTests: XCTestCase {
func testShouldFail() {
XCTFail("This will fail")
}
func testShouldPass() {
XCTAssertEqual(1,1)
}
}
let observer = PlaygroundTestObserver()
let center = XCTestObservationCenter.shared()
center.addTestObserver(observer)
TestRunner().runTests(testClass: ExampleTests.self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment