---
title: "require(_:_:sourceLocation:)"
framework: testing
role: symbol
role_heading: Macro
path: "testing/require(_:_:sourcelocation:)-6w9oo"
---

# require(_:_:sourceLocation:)

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

## Declaration

```swift
@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.

## Mentioned in

Migrating a test from XCTest

## Return Value

Return Value The unwrapped value of optionalValue.

## Overview

Overview note: An instance of ExpectationFailedError if optionalValue is nil. If optionalValue is nil, an Issue is recorded for the test that is running in the current task and an instance of ExpectationFailedError is thrown.

## See Also

### Checking expectations

- [expect(_:_:sourceLocation:)](testing/expect(_:_:sourcelocation:).md)
- [require(_:_:sourceLocation:)](testing/require(_:_:sourcelocation:)-5l63q.md)
