Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using XCTest in the swift repl or standalone script
// 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
You can’t perform that action at this time.