Forked from lzell/xctesting_in_repl_or_script.swift
Last active
September 6, 2018 15:49
-
-
Save dweinstein/eb8bd34a08bfebbeb3e0f3e39c4b3d24 to your computer and use it in GitHub Desktop.
Using XCTest in the swift repl or standalone script
This file contains 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 xcrun swift -F /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Frameworks/ | |
// Or run as script: | |
// $ xcrun swift -F xcrun swift -F /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Frameworks/ % | |
import Foundation | |
if dlopen("/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Frameworks/XCTest.framework/XCTest", RTLD_NOW) == nil { | |
perror(dlerror()) | |
} | |
if dlopen("/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/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