Skip to content

Commit 9bc49c5

Browse files
committed
Improve error reporting in edge cases.
When a failure happens after the test suite has stopped (or started), the failure message included the description of the class XCTestRun or XCTestCaseRun, which is the default description. Since those errors are difficult to trace back, instead of logging an unhelpful source/line location pointing to the inside of XCTest, log the additional information passed in into the method, to help the user to locate their error.
1 parent 3675a01 commit 9bc49c5

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Sources/XCTest/Public/XCTestRun.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,21 @@ open class XCTestRun {
119119
/// result of a failed assertion, `false` if it was the result of an
120120
/// uncaught exception.
121121
func recordFailure(withDescription description: String, inFile filePath: String?, atLine lineNumber: Int, expected: Bool) {
122+
func failureLocation() -> String {
123+
if let filePath = filePath {
124+
return "\(test.name) (\(filePath):\(lineNumber))"
125+
} else {
126+
return "\(test.name)"
127+
}
128+
}
129+
122130
guard isStarted else {
123131
fatalError("Invalid attempt to record a failure for a test run " +
124-
"that has not yet been started: \(self)")
132+
"that has not yet been started: \(failureLocation())")
125133
}
126134
guard !isStopped else {
127135
fatalError("Invalid attempt to record a failure for a test run " +
128-
"that has already been stopped: \(self)")
136+
"that has already been stopped: \(failureLocation())")
129137
}
130138

131139
if expected {

0 commit comments

Comments
 (0)