TestFixture

A test fixture is a state holder for a lazily initialized Value to be used in tests.

Fixtures come in two flavors:

A suite-level fixture provides a value with a lifetime of the test suite it was registered in. Its value is

All test elements within its suite share the same value from a suite-level fixture.

A test-level fixture provides its value to each single test with a lifetime of that test. Its value is passed

Each test gets its own fresh value from a test-level fixture.

Common characteristics:

  • The fixture lazily initializes its value on first use via the value lambda.

  • The value lambda can return any Kotlin object. The object expression can be used to return an anonymous composite value.

  • The value lambda is suspending. To create additional coroutines inside, see TestSuite.testSuiteCoroutineScope for details.

  • If Value is an AutoCloseable, the fixture will call close at the end of its lifetime, otherwise closeWith can specify an action to be called on close.

  • A fixture can be used either as a suite-level fixture, or as a test-level fixture, but not both.

Usage:

Types

Link copied to clipboard

A scope inside which tests receive a fixture-provided value.

Functions

Link copied to clipboard
infix fun asContextForAll(content: TestFixture.Scope<suspend Value.(testExecutionScope: Test.ExecutionScope) -> Unit>.() -> Unit): TestFixture<Value>

Provides the value from this fixture as a context for all tests declared in the content's scope.

Link copied to clipboard
infix fun asContextForEach(content: TestFixture.Scope<suspend Value.(testExecutionScope: Test.ExecutionScope) -> Unit>.() -> Unit): TestFixture<Value>

Provides a fresh value from this fixture as a context for each test declared in the content's scope.

Link copied to clipboard
infix fun asParameterForAll(content: TestFixture.Scope<suspend Test.ExecutionScope.(value: Value) -> Unit>.() -> Unit): TestFixture<Value>

Provides the value from this fixture as a parameter for all tests declared in the content's scope.

Link copied to clipboard
infix fun asParameterForEach(content: TestFixture.Scope<suspend Test.ExecutionScope.(value: Value) -> Unit>.() -> Unit): TestFixture<Value>

Provides a fresh value from this fixture as a parameter for each test declared in the content's scope.

Link copied to clipboard
infix fun closeWith(action: suspend Value.(testsSucceeded: Boolean) -> Unit): TestFixture<Value>

Registers action to be called when this fixture's lifetime ends.

Link copied to clipboard
suspend operator fun invoke(): Value

Returns the value of this fixture, which is implied to be a suite-level fixture.