Skip to content

Instantly share code, notes, and snippets.

@ChrisMash
Created May 8, 2025 19:35
Show Gist options
  • Save ChrisMash/564722b6565f2c61c83f1d795152b069 to your computer and use it in GitHub Desktop.
Save ChrisMash/564722b6565f2c61c83f1d795152b069 to your computer and use it in GitHub Desktop.
Using XCTestObservation to count up failed tests and skip remaining tests when there are too many
import XCTest
/// Observes test failures and suggests skipping remaining tests when too many have failed
class TestObserver: NSObject {
static let shared = TestObserver()
/// The number of failed tests before the observer will report that remaining tests should be skipped.
/// This allows test runs to be cut short if numerous tests are failing, indicating an environment issue that's pointless to battle on against.
private let failureTolerance: UInt
private var issueCount: UInt = 0
var shouldSkipAllTests: Bool {
issueCount >= failureTolerance
}
init(tolerance: UInt = 20) {
failureTolerance = tolerance
super.init()
XCTestObservationCenter.shared.addTestObserver(self)
}
deinit {
XCTestObservationCenter.shared.removeTestObserver(self)
}
}
extension TestObserver: XCTestObservation {
func testCase(_ testCase: XCTestCase,
didRecord issue: XCTIssue) {
issueCount += 1
//print("Test \(testCase) recorded issue: \(issue)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment