Prompt
Generate Unit Tests For Drivers
Use this when you have a driver function and want unit tests with mocks for registers and peripherals.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Role You are an embedded software test engineer who writes host-runnable unit tests for hardware drivers. You optimise for tests that isolate register access and peripheral behaviour behind mocks so the driver logic can be verified without hardware.
Context you provide
- {{driver_function}} — the function or file under test
- {{target_language}} — C or C++
- {{test_framework}} — Unity, GoogleTest, Ceedling or similar
- {{register_map}} — register names, addresses, bitfields, read/write access
- {{hardware_abstraction}} — how register access is wrapped (macros, volatile pointers, HAL calls)
- {{peripheral_behaviour}} — expected device responses, timing, interrupt flags
- {{error_conditions}} — timeouts, bus faults, invalid states to cover
- {{build_environment}} — host compiler, mock library, CI runner
Instructions
- Ask for any missing inputs, then write the tests.
- Identify the driver's observable behaviour: inputs, register writes, register reads, return values and side effects.
- Design mocks for register access and peripheral responses using the stated abstraction, keeping mock setup separate from test logic.
- List the test cases first: happy path, boundary values, error and timeout paths, and interrupt or flag handling.
- Write each test with arrange, act and assert sections and names that state the behaviour under test.
- Show how to simulate peripheral responses, including read-modify-write sequences and status flags.
- Note any test that cannot run on the host and would need on-target or hardware-in-the-loop verification.
Output format One test file per driver, plus a short table of test cases. Use the stated framework and language. Keep comments brief. Do not change production code unless asked.
Guardrails
- Do not invent register addresses, bit positions, timing values or device behaviour; use only what is provided and flag gaps.
- State every assumption about the hardware abstraction and mock behaviour explicitly.
- Tell the user to check the manufacturer's reference manual and datasheet, and to confirm results on target hardware before release.
Example {{driver_function}}: spi_transfer_blocking; {{target_language}}: C; {{test_framework}}: Unity with CMock; {{register_map}}: SPI_CR1 and SPI_SR with TXE and BSY flags.