Contents

scopeProvider(for:testCase:)

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

Declaration

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 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, the default value returned by this method depends on the values oftest and testCase:

  • If test represents a suite, this trait must conform to SuiteTrait. If the value of this suite trait’s 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 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. 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:).

If this trait’s type does not conform to TestScoping and its associated 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.

See Also

Running code before and after a test or suite