Contents

cancel(_:sourceLocation:)

Cancel the current test or test case.

Declaration

static func cancel(_ comment: Comment? = nil, sourceLocation: SourceLocation = #_sourceLocation) throws -> Never

Parameters

  • comment:

    A comment describing why you are cancelling the test or test case.

  • sourceLocation:

    The source location to which the testing library will attribute the cancellation.

Mentioned in

Discussion

The testing library runs each test and each test case in its own task. When you call this function, the testing library cancels the task associated with the current test:

@Test func `Food truck is well-stocked`() throws {
  guard businessHours.contains(.now) else {
    try Test.cancel("We're off the clock.")
  }
  // ...
}

If the current test is a parameterized test function, this function instead cancels the current test case. Other test cases in the test function are not affected.

If the current test is a suite, the testing library cancels all of its pending and running tests.

If you have already cancelled the current test or if it has already finished running, this function throws an error to indicate that the current test has been cancelled, but does not attempt to cancel the test a second time.