Test-Driven Development — Custom Logging during runtime

Romain Brunie
2 min readFeb 22, 2020

--

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.

testBundleWillStart(_:) gets called at the beginning of a test bundle.
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.
testCase(_:didFailWithDescription:inFile:atLine:) 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.

Source

Sign up to discover human stories that deepen your understanding of the world.

--

--

Romain Brunie
Romain Brunie

Written by Romain Brunie

Passionate about Clean Code and Software Craftsmanship @AVIV

No responses yet

Write a response