Back to Quick

StopTest

docs/docsets/Quick.docset/Contents/Resources/Documents/Structs/StopTest.html

7.6.22.8 KB
Original Source

StopTest

public struct StopTest : Error

Stops execution of test when thrown inside an it block, emitting a failure message.

Unlike other errors that can be thrown during a test, StopTest represents an expected failure, with the failure description tied to the file and line it is thrown on.

Tests can also be stopped silently by throwing StopTest.silently.

The primary use case of StopTest as opposed to normal error logging is when a condition is critical for the remainder of the test. It serves as an alternative to force unwrapping or out-of-range subscripts that could be cause the test to crash.

For example,

guard let value = getValue() else {
    throw StopTest("Got a null value from `getValue()`)
}

When used with Nimble, any expectation can stop a test by adding .onFailure(throw: StopTest.silently).

For example,

try expect(array).toEventually(haveCount(10)).onFailure(throw: StopTest.silently)

`

                failureDescription
                `

Undocumented

Declaration

Swift

public let failureDescription: String

`

                reportError
                `

Undocumented

Declaration

Swift

public let reportError: Bool

`

                callsite
                `

Undocumented

Declaration

Swift

public let callsite: Callsite

`

                init(_:file:line:)
                `

Returns a new StopTest instance that, when thrown, stops the test and logs an error.

Declaration

Swift

public init(_ failureDescription: String, file: FileString = #file, line: UInt = #line)

Parameters

| failureDescription |

The message to display in the test results.

| | file |

The absolute path to the file containing the error. A sensible default is provided.

| | line |

The line containing the error. A sensible default is provided.

|

`

                silently
                `

An error that, when thrown, stops the test without logging an error.

This is meant to be used alongside methods that have already logged a test failure.

For example,

func checkProperty() -> Bool {
    if property.isValid {
        return true
    } else {
        XCTFail("\(property) is not valid")
        return false
    }
}

guard checkProperty() else {
    throw StopTest.error
}

Declaration

Swift

public static let silently: StopTest

© 2025 Quick Contributors. All rights reserved. (Last updated: 2025-06-26)

Generated by jazzy ♪♫ v0.15.3, a Realm project.