Skip to content

Commit 8fc4dfa

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 8fc4dfa

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Sources/XCTest/XCTestCase.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
// Base protocol (and extension with default methods) for test cases
1212
//
1313

14+
#if os(Linux) || os(FreeBSD)
15+
import Glibc
16+
#else
17+
import Darwin
18+
#endif
19+
20+
1421
public protocol XCTestCase : XCTestCaseProvider {
1522
func setUp()
1623
func tearDown()
@@ -47,6 +54,7 @@ extension XCTestCase {
4754
}
4855

4956
print("Test Case '\(method)' started.")
57+
fflush(stdout)
5058

5159
setUp()
5260

@@ -74,6 +82,7 @@ extension XCTestCase {
7482
}
7583

7684
print("Test Case '\(method)' \(result) (\(printableStringForTimeInterval(duration)) seconds).")
85+
fflush(stdout)
7786
XCTAllRuns.append(XCTRun(duration: duration, method: method, passed: failures.count == 0, failures: failures))
7887
XCTFailureHandler = nil
7988
}
@@ -89,6 +98,7 @@ extension XCTestCase {
8998
}
9099

91100
print("Executed \(tests.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(unexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
101+
fflush(stdout)
92102
}
93103

94104
public func setUp() {

Sources/XCTest/XCTestMain.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct XCTFailure {
2727

2828
func emit(method: String) {
2929
print("\(file):\(line): \(expected ? "" : "unexpected ")error: \(method) : \(failureDescription) - \(message)")
30+
fflush(stdout)
3031
}
3132
}
3233

@@ -63,6 +64,7 @@ internal struct XCTRun {
6364
}
6465

6566
print("Total executed \(XCTAllRuns.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(totalUnexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds")
67+
fflush(stdout)
6668
exit(totalFailures > 0 ? 1 : 0)
6769
}
6870

0 commit comments

Comments
 (0)