> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dairy.foundation/llms.txt
> Use this file to discover all available pages before exploring further.

# Unit Testing OpModes with Dairy

Dairy supports a unit test runtime, to use it you need to add Dairy's test
fixtures as a test dependency:

Add the newest version of Core found
[here](https://repo.dairy.foundation/#/releases/dev/frozenmilk/dairy/Core) to
the `dependencies` block:

```gradle theme={null}
depdependencies {
	// ...
	testImplementation testFixtures("dev.frozenmilk.dairy:Core:<MAJ.MIN.ENG>")
	// ...
}
```

Then, you'll need to set up a unit test source set, and add the
`org.firstinspires.ftc.teamcode` packages.

Test OpModes can be combined with Features to test both that OpModes behave as
expected and that Features behave as expected.

# Writing an OpMode for testing

This is super useful for library authors working with Dairy.

## TestOpMode

<CodeGroup>
  ```java Java theme={null}
  @RunWith(OpModeTestRunner.class)
  public class IterativeTest extends TestOpMode {
  	@Override
  	public void init() {
  		// do w/e
  	}
  }
  ```

  ```kt Kotlin theme={null}
  @RunWith(OpModeTestRunner::class)
  class IterativeTest : TestOpMode() {
  	override fun init() {
  		// do w/e
  	}
  }
  ```
</CodeGroup>

## TestLinearOpMode

<CodeGroup>
  ```java Java theme={null}
  @RunWith(OpModeTestRunner.class)
  public class LinearTest extends TestLinearOpMode {
  	@Override
  	public void runOpMode() throws InterruptedException {
  		// do w/e
  	}
  }
  ```

  ```kt Kotlin theme={null}
  @RunWith(OpModeTestRunner::class)
  class LinearTest : TestLinearOpMode() {
  	override fun runOpMode() {
  		// do w/e
  	}
  }
  ```
</CodeGroup>

# Running a teamcode OpMode as a test

At the moment this is a bit limited, as there is no direct support for mocking
hardware or telemetry, but its possible for you to do this yourself, and support
for doing so easily is on the roadmap.

<CodeGroup>
  ```java Java theme={null}
  @RunWith(OpModeTestRunner.class)
  public class TeamCodeTest extends ExternalTest {
  	public TeamCodeTest() {
  		// we'll use an OpMode we defined, but we can use one from teamcode
  		super(IterativeTest.class);
  	}
  }
  ```

  ```kt Kotlin theme={null}
  @RunWith(OpModeTestRunner::class)
  class TeamCodeTest : ExternalTest(IterativeTest::class.java)
  ```
</CodeGroup>

# Configuring Test OpModes

Lastly, we can configure OpModes to do things like setup hardware.

<CodeGroup>
  ```java Java theme={null}
  public class OpModeConfig extends TestOpModeConfiguration {
  	private OpModeConfig() {}
  	public static OpModeConfig INSTANCE = new OpModeConfig();
  	@Override
  	protected void configureOpMode(@NonNull OpMode opMode) {
  		opMode.hardwareMap.put("a", (HardwareDevice) null);
  	}
  }
  ```

  ```kt Kotlin theme={null}
  object OpModeConfig : TestOpModeConfiguration() {
  	override fun configureOpMode(opMode: OpMode) {
  		opMode.hardwareMap.put("a", null as HardwareDevice)
  	}
  }
  ```
</CodeGroup>
