Skip to content

Instantly share code, notes, and snippets.

@ChrisMash
ChrisMash / using_xctestobservation.swift
Created May 8, 2025 19:38
Skipping UI tests when too many have failed with XCTestObservation
override func setUpWithError() throws {
continueAfterFailure = false
if TestObserver.shared.shouldSkipAllTests {
throw XCTSkip("Too many tests have failed, skipping remainder")
}
}
@ChrisMash
ChrisMash / xctestobservation.swift
Created May 8, 2025 19:35
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
@ChrisMash
ChrisMash / ut_vertify_prereq_improved.swift
Last active October 16, 2023 19:33
A UT that does an improved job of verifying pre-requisites
verify(StorageThing)
.hasNoEntry("something")
sut = UnitBeingTested()
verify(StorageThing)
.hasEntry("something")
@ChrisMash
ChrisMash / ut_vertify_prereq_poor.swift
Last active October 16, 2023 19:32
A UT that does a poor job of verifying pre-requisites
sut = UnitBeingTested()
verify(StorageThing)
.hasEntry("something")
@ChrisMash
ChrisMash / ut_verify_expected_behaviour_poor.swift
Last active October 16, 2023 19:32
A UT that does a poor job of verifying expected behaviour
sut = UnitBeingTested("a value")
verify(sut.value)
.is("a value")
@ChrisMash
ChrisMash / ut_verify_unexpected_functions_improved.swift
Last active October 16, 2023 19:32
A UT that does an improved job of verifying unexpected function calls aren't made
delegate = ADelegate()
sut = UnitBeingTested(delegate)
sut.aFunctionCall()
verify(delegate)
.callMadeTo(onSuccess)
verify(delegate)
.callNOTMadeTo(onError)
@ChrisMash
ChrisMash / ut_verify_unexpected_functions_poor.swift
Last active October 16, 2023 19:31
A UT that does a poor job of verifying unexpected function calls aren't made
delegate = ADelegate()
sut = UnitBeingTested(delegate)
sut.aFunctionCall()
verify(delegate)
.callMadeTo(onSuccess)
@ChrisMash
ChrisMash / ut_verify_expected_values_improved.swift
Last active October 16, 2023 19:31
A UT that does an improved job of verifying expected values
delegate = ADelegate()
sut = UnitBeingTested(delegate)
sut.aFunctionCall()
verify(delegate)
.callMadeTo(functionOnDelegate)
.withParameter("1234")