Skip to content

Commit 8cd00ce

Browse files
committed
flush stdout after any printed test result
When running tests from wrappers, such as continuous integration servers, the output of tests may be incorrectly buffered that will result in truncated displaying of tests when failures occur. This behavior incorrectly identifies the wrong test as the failure point in the suites, enforcing a fflush of stdout ensures the buffer is correctly flushed after each marker point for testing.
1 parent 471f31a commit 8cd00ce

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Sources/XCTest/XCTestCase.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extension XCTestCase {
4646
}
4747
}
4848

49-
print("Test Case '\(method)' started.")
49+
XCTPrint("Test Case '\(method)' started.")
5050

5151
setUp()
5252

@@ -73,7 +73,7 @@ extension XCTestCase {
7373
result = failures.count > 0 ? "failed" : "passed"
7474
}
7575

76-
print("Test Case '\(method)' \(result) (\(printableStringForTimeInterval(duration)) seconds).")
76+
XCTPrint("Test Case '\(method)' \(result) (\(printableStringForTimeInterval(duration)) seconds).")
7777
XCTAllRuns.append(XCTRun(duration: duration, method: method, passed: failures.count == 0, failures: failures))
7878
XCTFailureHandler = nil
7979
}
@@ -88,7 +88,7 @@ extension XCTestCase {
8888
failureSuffix = ""
8989
}
9090

91-
print("Executed \(tests.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(unexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
91+
XCTPrint("Executed \(tests.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(unexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
9292
}
9393

9494
public func setUp() {

Sources/XCTest/XCTestMain.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import Glibc
1818
import Darwin
1919
#endif
2020

21+
internal func XCTPrint(items: Any..., separator: String = " ", terminator: String = "\n") {
22+
print(items, separator: separator, terminator: terminator)
23+
fflush(stdout)
24+
}
25+
2126
struct XCTFailure {
2227
var message: String
2328
var failureDescription: String
@@ -26,7 +31,7 @@ struct XCTFailure {
2631
var line: UInt
2732

2833
func emit(method: String) {
29-
print("\(file):\(line): \(expected ? "" : "unexpected ")error: \(method) : \(failureDescription) - \(message)")
34+
XCTPrint("\(file):\(line): \(expected ? "" : "unexpected ")error: \(method) : \(failureDescription) - \(message)")
3035
}
3136
}
3237

@@ -62,7 +67,7 @@ internal struct XCTRun {
6267
failureSuffix = ""
6368
}
6469

65-
print("Total executed \(XCTAllRuns.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(totalUnexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
70+
XCTPrint("Total executed \(XCTAllRuns.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(totalUnexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
6671
exit(totalFailures > 0 ? 1 : 0)
6772
}
6873

0 commit comments

Comments
 (0)