Test-Driven Development — Custom Logging during runtime

XCTestObservation Protocol
The
XCTestObservation
protocol defines methods that are called in response to significant events in the progress of test runs.
In order to implement your XCTestObservation
protocol, you first need to create a class that inherit from NSObject
and XCTestObservation
.
import XCTestclass LoggingObserver: NSObject, XCTestObservation { override init() {
super.init()
XCTestObservationCenter.shared.addTestObserver(self)
} func testBundleWillStart(_ testBundle: Bundle) {
print("testBundleWillStart")
} ...
}
Then go to the Info.plist
in your test target. Add an entry with the Key NSPrincipalClass
and the value $(PRODUCT_NAME).LoggingObserver
(change $(PRODUCT_NAME)
with your module name). You are set up and you can now implement the delegate methods you want from XCTestObservation.


testSuiteWillStart(_:)
gets called at the beginning of each test suite.
testCaseWillStart(_:) gets called at the beginning of each test case.

testCaseDidFinish(_:) gets called at the end of each test case.

gets called when a test case fails.

testSuiteDidFinish(_:) gets called at the end of each test suite.

testBundleDidFinish(_:) gets called at the end of a test bundle.