<!--
{
  "availability" : [
    "Xcode: 16.0.0 -",
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "swift: 6.0.0 -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "Testing",
  "identifier" : "/documentation/Testing/require(_:_:sourceLocation:)-6w9oo",
  "metadataVersion" : "0.1.0",
  "role" : "Macro",
  "symbol" : {
    "kind" : "Macro",
    "modules" : [
      "Swift Testing"
    ],
    "preciseIdentifier" : "s:7Testing7require__14sourceLocationxxSg_AA7CommentVSgyXKAA06SourceD0Vtclufm"
  },
  "title" : "require(_:_:sourceLocation:)"
}
-->

# require(_:_:sourceLocation:)

Unwrap an optional value or, if it is `nil`, fail and throw an error.

```
@freestanding(expression) macro require<T>(_ optionalValue: T?, _ comment: @autoclosure () -> Comment? = nil, sourceLocation: SourceLocation = #_sourceLocation) -> T
```

## Parameters

`optionalValue`

The optional value to be unwrapped.

`comment`

A comment describing the expectation.

`sourceLocation`

The source location to which recorded expectations and
issues should be attributed.

## Return Value

The unwrapped value of `optionalValue`.

## Overview> Throws: An instance of ``doc://org.swift.testing/documentation/Testing/ExpectationFailedError`` if `optionalValue` is
> `nil`.

If `optionalValue` is `nil`, an [`Issue`](/documentation/Testing/Issue) is recorded for the test that is
running in the current task and an instance of [`ExpectationFailedError`](/documentation/Testing/ExpectationFailedError) is
thrown.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)