Created
May 8, 2025 19:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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