Created
August 11, 2016 20:01
-
-
Save lzell/3a4f43d00657a347363343c035e5db4c to your computer and use it in GitHub Desktop.
Using XCTest in the swift repl or standalone script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start repl with: | |
// $ xcrun swift -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks | |
// Or run as script: | |
// $ xcrun swift -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks % | |
import Foundation | |
if dlopen("/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Versions/A/XCTest", RTLD_NOW) == nil { | |
perror(dlerror()) | |
} | |
if dlopen("/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftXCTest.dylib", RTLD_NOW) == nil { | |
perror(dlerror()) | |
} | |
import XCTest | |
class ATest : XCTestCase { | |
func testIt() { | |
XCTAssertTrue(false, "Now I see this!") | |
} | |
func testThat() { | |
XCTAssertNil(nil, "Passes!") | |
} | |
} | |
class BTest : XCTestCase { | |
func testIt() { | |
XCTAssertFalse(false, "whatever") | |
} | |
func testThat() { | |
XCTAssertEqual(1, 0, "I fail too") | |
} | |
} | |
// MARK: - | |
func selectors<T: XCTestCase>(forType type: T.Type) -> [Selector] { | |
var selectors = [Selector]() | |
var count : UInt32 = 0 | |
let methods = class_copyMethodList(type, &count)! | |
for i in 0..<count { | |
let method = methods.advanced(by: Int(i)).pointee! | |
selectors.append(method_getName(method)!) | |
} | |
return selectors | |
} | |
func runTests(_ types: XCTestCase.Type...) { | |
let suite = XCTestSuite(name: "Required") | |
for t in types { | |
let tests = selectors(forType: t).filter() { String($0).hasPrefix("test") } | |
tests.map(t.init) | |
.forEach(suite.addTest) | |
} | |
suite.run() | |
} | |
runTests(ATest.self, BTest.self) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment