Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import UIKit
import PlaygroundSupport
import XCTest
struct Person {
let firstName: String
let lastName: String
let address: String
}
struct ViewModel {
var person: Person
var nameAndAddress: String {
return "\(person.firstName) \(person.lastName), \(person.address)"
}
init() {
person = Person(firstName: "John",
lastName: "Appleseed",
address: "1 Infinite Loop, Cupertino, CA")
}
}
let label = UILabel()
label.text = ViewModel().nameAndAddress
label.backgroundColor = UIColor.white
label.sizeToFit()
PlaygroundPage.current.liveView = label
class PersonTest: XCTestCase {
override func setUp() {
super.setUp()
}
override func tearDown() {
super.tearDown()
}
func testResultOfNameAndAddressFunc() {
XCTAssertEqual(ViewModel().nameAndAddress,
"John Appleseed, 1 Infinite Loop, Cupertino, CA")
}
}
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)")
}
}
let observer = PlaygroundTestObserver()
let center = XCTestObservationCenter.shared()
center.addTestObserver(observer)
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)s with \(run.totalFailureCount) failures")
}
}
TestRunner().runTests(testClass: PersonTest.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.