Unit Testing for FRC Robot Code
What is Unit Testing?
A unit test checks one class or method in isolation. Tests run on your laptop—not the roboRIO—and finish in milliseconds.
Use them to verify math, state machines, and command logic without a robot on the field. Passing tests also document expected behavior when you refactor later.
Learn more: WPILib: Unit Testing
Why Unit Testing is Important
Early bugs: Catch logic and edge-case errors before deploy.
Refactoring: Tests flag behavior that changed when you clean up code.
Documentation: A test class shows how a subsystem should respond.
Speed: gradlew test finishes in seconds; deploy takes minutes.
Rare states: Mock a disconnected sensor or stalled motor without risking hardware.
Testing Frameworks
WPILib projects use JUnit 5 for assertions and test lifecycle hooks.
Mockito builds fake motor and sensor objects so subsystem tests do not need real hardware.
Basic Unit Test Example
package frc.robot.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class MathUtilsTest {
@Test
public void testClamp() {
// Assert that clamp(val, min, max) returns expected results
assertEquals(5.0, MathUtils.clamp(10.0, 0.0, 5.0), 0.001, "Should clamp to max");
assertEquals(0.0, MathUtils.clamp(-5.0, 0.0, 5.0), 0.001, "Should clamp to min");
assertEquals(3.0, MathUtils.clamp(3.0, 0.0, 5.0), 0.001, "Should return value within range");
}
@Test
public void testDeadband() {
assertEquals(0.0, MathUtils.applyDeadband(0.05, 0.1), 0.001, "Should be zero inside deadband");
assertEquals(0.5, MathUtils.applyDeadband(0.5, 0.1), 0.001, "Should return value outside deadband");
}
}
Testing Subsystems with Mocks
Subsystems depend on hardware types such as TalonFX or SparkMax. Inject mocks so tests can verify the subsystem called set with the expected value without a motor on the bus.
Subsystem Test with Mockito
package frc.robot.subsystems;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.revrobotics.spark.SparkMax;
import frc.robot.Constants;
public class IntakeTest {
private SparkMax m_mockMotor;
private Intake m_intake;
@BeforeEach
void setup() {
// Create a mock SparkMax
m_mockMotor = mock(SparkMax.class);
// Inject the mock into the subsystem
m_intake = new Intake(m_mockMotor);
}
@Test
void testDeploy() {
m_intake.deploy();
// Verify the motor was set to the deploy speed
verify(m_mockMotor).set(Constants.Intake.kDeploySpeed);
}
@Test
void testStop() {
m_intake.stop();
// Verify the motor was stopped
verify(m_mockMotor).set(0.0);
}
}
Testing Commands and Time
Commands that wait on time need simulated clocks. WPILib SimHooks advances robot time so a two-second WaitCommand can be tested in one test method. Schedule commands through CommandScheduler and assert when they finish or cancel.
Command and Time Simulation
package frc.robot.commands;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import edu.wpi.first.wpilibj.simulation.SimHooks;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.WaitCommand;
public class CommandTest {
@Test
void testWaitCommand() {
CommandScheduler scheduler = CommandScheduler.getInstance();
WaitCommand waitCmd = new WaitCommand(2.0);
scheduler.schedule(waitCmd);
scheduler.run();
assertTrue(scheduler.isScheduled(waitCmd), "Command should be scheduled");
// Advance time by 1 second
SimHooks.stepInTime(1.0);
scheduler.run();
assertTrue(scheduler.isScheduled(waitCmd), "Command should still be running after 1s");
// Advance time by another 1.1 seconds (total 2.1)
SimHooks.stepInTime(1.1);
scheduler.run();
assertFalse(scheduler.isScheduled(waitCmd), "Command should finish after 2s");
}
}
Best Practices for FRC Unit Testing
Follow these guidelines for effective tests:
- Isolate tests: Each test should run independently and not depend on the state of others.
- Mock hardware: Don’t rely on real hardware classes; mock them or use simulation classes.
- Test edge cases: Test boundary values (0, max, min) and invalid inputs.
- Keep it fast: Unit tests should run instantly. If it takes seconds, it’s too slow.
- Use dependency injection: Pass hardware objects into subsystem constructors to make mocking easier.
- Run often: Run tests before every commit and push.