---
title: "AndroidComposeUiTestEnvironment"
description: "A test environment that can [run tests][runTest] using the [test receiver scope][test]. Note that
some of the properties and methods on [test] will only work during the call to [runTest], as they
require that the environment has been set up.

If the [effectContext] contains a [TestDispatcher], that dispatcher will be used to run
composition, and its [TestCoroutineScheduler] will be used to construct the [MainTestClock]. If
the `effectContext` does not contain a `TestDispatcher`, a [StandardTestDispatcher] will be
created for `androidx.compose.ui.test.v2.*` APIs; otherwise, an [UnconfinedTestDispatcher] will
be created. In both cases, the `TestCoroutineScheduler` from the `effectContext` will be used if
present."
type: "class"
---

<div class='type'>Class</div>


<a id='references'></a>

<div class='sourceset sourceset-android'>Android</div>


```kotlin
@ExperimentalTestApi
abstract class AndroidComposeUiTestEnvironment<A : ComponentActivity>
internal constructor(
    private val effectContext: CoroutineContext = EmptyCoroutineContext,
    private val runTestContext: CoroutineContext = EmptyCoroutineContext,
    private val testTimeout: Duration = 60.seconds,
    private val useStandardTestDispatcherForComposition: Boolean,
)
```


A test environment that can `run tests` using the `test receiver scope`. Note that
some of the properties and methods on `test` will only work during the call to `runTest`, as they
require that the environment has been set up.

If the `effectContext` contains a `TestDispatcher`, that dispatcher will be used to run
composition, and its `TestCoroutineScheduler` will be used to construct the `MainTestClock`. If
the `effectContext` does not contain a `TestDispatcher`, a `StandardTestDispatcher` will be
created for `androidx.compose.ui.test.v2.*` APIs; otherwise, an `UnconfinedTestDispatcher` will
be created. In both cases, the `TestCoroutineScheduler` from the `effectContext` will be used if
present.

#### Parameters

| | |
| --- | --- |
| A | The Activity type to be interacted with, which typically (but not necessarily) is the activity that was launched and hosts the Compose content. |
| effectContext | The `CoroutineContext` used to run the composition. The context for `LaunchedEffect`s and `rememberCoroutineScope` will be derived from this context. If this context contains a `TestDispatcher` or `TestCoroutineScheduler` (in that order), it will be used for composition and the `MainTestClock`. |
| runTestContext | The `CoroutineContext` used to create the context to run the test. By default it will run using `kotlinx.coroutines.test.StandardTestDispatcher`. `runTestContext` and `effectContext` must not share `TestCoroutineScheduler`. |
| testTimeout | The `Duration` within which the test is expected to complete, otherwise a platform specific timeout exception will be thrown. |



## Secondary Constructors

```kotlin
constructor() : this(EmptyCoroutineContext)
```

```kotlin
constructor(
    effectContext: CoroutineContext = EmptyCoroutineContext
) : this(effectContext, EmptyCoroutineContext, 60.seconds)
```

```kotlin
constructor(
    effectContext: CoroutineContext = EmptyCoroutineContext,
    runTestContext: CoroutineContext = EmptyCoroutineContext,
    testTimeout: Duration = 60.seconds,
) : this(effectContext, runTestContext, testTimeout, true)
```

## Properties

<div class='sourceset sourceset-android'>Android</div>


```kotlin
val test: AndroidComposeUiTest<A>
```


The receiver scope of the test passed to `runTest`. Note that some of the properties and
methods will only work during the call to `runTest`, as they require that the environment has
been set up.



## Functions

```kotlin
fun cancelAndRecreateRecomposer()
```


Recreates the CoroutineContext associated with Compose being cancelled. This happens when an
app moves from a regular ("Full screen") view of the app to a "Pop up" view AND certain
properties in the manifest's android:configChanges are set to prevent a full tear down of the
app. This is a somewhat rare case (see issuetracker.google.com/issues/309326720 for more
details).

It does this by canceling the existing Recomposer and creates a new Recomposer (including a
new recomposer coroutine scope for that new Recomposer) and a new ComposeIdlingResource.

To see full test:
click_viewAddedAndRemovedWithRecomposerCancelledAndRecreated_clickStillWorks


```kotlin
fun <R> runTestNonSuspendingLambda(block: AndroidComposeUiTest<A>.() -> R?): R?
```

```kotlin
fun <R> runTest(block: suspend AndroidComposeUiTest<A>.() -> R): TestResult
```


Runs the given `block`, setting up all test hooks before running the test and tearing them
down after running the test.


## Companion Object

#### Properties

<div class='sourceset sourceset-android'>Android</div>


```kotlin
val TAG = "ComposeUiTest"
```




