<!--
{
  "availability" : [
    "Swift: 6.1.0 -",
    "Xcode: 16.3.0 -",
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "swift: 6.0.0 -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "Testing",
  "identifier" : "/documentation/Testing/Trait/scopeProvider(for:testCase:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift Testing"
    ],
    "preciseIdentifier" : "s:7Testing5TraitP13scopeProvider3for8testCase09TestScopeD0QzSgAA0H0V_AK0G0VSgtF"
  },
  "title" : "scopeProvider(for:testCase:)"
}
-->

# scopeProvider(for:testCase:)

Get this trait’s scope provider for the specified test and optional test
case.

```
func scopeProvider(for test: Test, testCase: Test.Case?) -> Self.TestScopeProvider?
```

## Parameters

`test`

The test for which a scope provider is being requested.

`testCase`

The test case for which a scope provider is being requested,
if any. When `test` represents a suite, the value of this argument is
`nil`.

## Return Value

A value conforming to [`TestScopeProvider`](/documentation/Testing/Trait/TestScopeProvider) which you
use to provide custom scoping for `test` or `testCase`. Returns `nil` if
the trait doesn’t provide any custom scope for the test or test case.

## Discussion

If this trait’s type conforms to [`TestScoping`](/documentation/Testing/TestScoping), the default value
returned by this method depends on the values of`test` and `testCase`:

- If `test` represents a suite, this trait must conform to [`SuiteTrait`](/documentation/Testing/SuiteTrait).
  If the value of this suite trait’s [`isRecursive`](/documentation/Testing/SuiteTrait/isRecursive) property
  is `true`, then this method returns `nil`, and the suite trait
  provides its custom scope once for each test function the test suite
  contains. If the value of [`isRecursive`](/documentation/Testing/SuiteTrait/isRecursive) is `false`, this
  method returns `self`, and the suite trait provides its custom scope
  once for the entire test suite.
- If `test` represents a test function, this trait also conforms to
  [`TestTrait`](/documentation/Testing/TestTrait). If `testCase` is `nil`, this method returns `nil`;
  otherwise, it returns `self`. This means that by default, a trait which
  is applied to or inherited by a test function provides its custom scope
  once for each of that function’s cases.

A trait may override this method to further customize the
default behaviors above. For example, if a trait needs to provide custom
test scope both once per-suite and once per-test function in that suite,
it implements the method to return a non-`nil` scope provider under
those conditions.

A trait may also implement this method and return `nil` if it determines
that it does not need to provide a custom scope for a particular test at
runtime, even if the test has the trait applied. This can improve
performance and make diagnostics clearer by avoiding an unnecessary call
to [`provideScope(for:testCase:performing:)`](/documentation/Testing/TestScoping/provideScope(for:testCase:performing:)).

If this trait’s type does not conform to [`TestScoping`](/documentation/Testing/TestScoping) and its
associated [`TestScopeProvider`](/documentation/Testing/Trait/TestScopeProvider) type is the default `Never`, then
this method returns `nil` by default. This means that instances of this
trait don’t provide a custom scope for tests to which they’re applied.

---

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)