Can an LLM write unit tests as effective as human-written ones?A comparison against NASA's own tests for its flight software
Two Claude models and three local models wrote unit tests for NASA's core Flight System. I measured each test file exactly as I measured NASA's own hand-written file for the same source.
Yes. The strongest models now write unit tests as effective as the ones NASA's engineers wrote for the same code.
Across 22 source files of flight software, Claude's test files matched or exceeded NASA's on about nine files in ten. The best local model managed just over half. Everything below is the evidence, and the caveats.
Share of attempted files where the model's tests were at least as effective as NASA's
What “at least as effective” means. The model's test file builds, passes against the real source, and detects at least as many planted faults as NASA's file for the same source. That last measure is mutation testing: plant a small fault, re-run the tests, see whether anything fails.
One more angle, kept separate. I also put 15 real bugs from the project's history back into the code. Sonnet's tests detected 13 and NASA's 7. That is a narrower question than the one in the title, and it has its own section.
Why NASA's code
It is easy to ask a model for unit tests and get something that compiles and turns green. The useful question is whether those tests are as good as the ones a careful engineer would have written for the same code.
NASA's core Flight System (cFS) makes that question answerable. It is open source, written in C, flown on real missions, and ships with a hand-written unit test file for nearly every source file. So for any source file there is a human-written test file to compare against, written by the people who own the code.
What was measured
Every test file, whether a model wrote it or NASA did, goes through the same three measurements.
- Does it build and pass? The file is compiled in a container with warnings as errors, then run against the real source.
- How much does it cover? Line and branch coverage of the file under test. NASA's tests reach 100% of both on every file used here.
- How many faults does it detect? Small faults are planted in the source one at a time: a flipped comparison, a dropped assignment, a removed function call. The tests are re-run, and if any check fails the fault is detected. This is mutation testing, a standard way to measure how strong a test suite is, as opposed to how much code it touches.
I count a model's test file as at least as effective as NASA's when it builds, passes, and detects at least as many planted faults as NASA's file for the same source. Coverage is reported next to that, and I also give a stricter count that requires full coverage.
How the models were run
- From scratch. The model sees one source file, the app's headers and test helpers, and one example test file for style. It never sees NASA's test file for the source it is testing.
- Three attempts. If the file does not build or a check fails, the model gets the compiler errors or failing checks and may try again, twice.
- Same scoring for everyone. NASA's file and the model's file are built and measured by the same script in the same container.
Result one: ten files, five models
Ten source files from two cFS apps, Housekeeping (HK) and Memory Dwell (MD), with 244 planted faults between them. All five models attempted all ten.
Test files at least as effective as NASA's, out of 10
Planted faults detected, out of 244
Planted faults detected, by file
| Source file | Planted faults | NASA | Opus 5.5 | Sonnet 5.5 | Qwen4 | Gemma 8-bit |
|---|---|---|---|---|---|---|
| hk_app.c | 28 | 26 | 28 | failed | failed | failed |
| hk_cmds.c | 12 | 10 | 12 | 12 | 12 | failed |
| hk_dispatch.c | 28 | 24 | 28 | 26 | failed | failed |
| hk_utils.c | 24 | 18 | 23 | 23 | out of memory | failed |
| md_app.c | 26 | 16 | 23 | 21 | failed | failed |
| md_cmds.c | 26 | 14 | 22 | 22 | 20 | failed |
| md_dispatch.c | 25 | 20 | 22 | 22 | failed | failed |
| md_dwell_pkt.c | 26 | 15 | 26 | 26 | 23 | failed |
| md_dwell_tbl.c | 23 | 12 | 22 | 22 | 16 | failed |
| md_utils.c | 26 | 24 | 23 | 23 | 23 | 24 |
Opus produced a passing file for all ten sources. Nine detected more faults than NASA's file; the tenth, for md_utils.c, was one fault short. Sonnet matched or exceeded NASA's on eight. It was one fault short on the same file and never got hk_app.c to pass.
Qwen4 passed five files. Four of them matched or exceeded NASA's and the fifth was one fault short. Of the other five, four never passed and one ran out of memory. No Qwen4 file passed on its first attempt.
Gemma 4 struggled with the framework itself. The 8-bit build passed one file, which tied NASA's. The 6-bit build passed none, and only two of its thirty attempts compiled.
On coverage, NASA's files reach 100% of lines and branches everywhere. So did 9 of Opus's 10 passing files, 8 of Sonnet's 9 and 3 of Qwen4's 5. The rest came within a few percent.
Result two: twelve larger files
A second set of 12 source files from six other apps (CF, CS, DS, FM, HS and SC), with 279 planted faults. These files are larger, and here the style example came from a different app, so the model saw no NASA test file from the app it was testing.
Not every model ran every file. Sonnet ran all twelve. Opus ran two before my API credit ran out. Qwen4 ran the four whose prompts fit its context window on this machine.
Planted faults detected on the second set, by file
| Source file | Planted faults | NASA | Sonnet 5.5 | Opus 5.5 | Qwen4 |
|---|---|---|---|---|---|
| cf_chunk.c | 25 | 16 | 17 | 17 | too large |
| cf_codec.c | 19 | 15 | 18 | 18 | too large |
| cs_compute.c | 23 | 18 | 23 | not run | too large |
| cs_table_processing.c | 25 | 16 | 25 | not run | too large |
| ds_app.c | 26 | 18 | 23 | not run | 20 |
| fm_app.c | 25 | 14 | 25 | not run | too large |
| fm_cmds.c | 24 | 10 | 18 | not run | too large |
| hs_app_monitor.c | 24 | 16 | 21 | not run | 19 |
| hs_event_monitor.c | 24 | 17 | 21 | not run | 18 |
| hs_exec_monitor.c | 21 | 11 | 16 | not run | 16 |
| sc_app.c | 22 | 11 | 20 | not run | too large |
| sc_atsrq.c | 21 | 13 | 21 | not run | too large |
Every file that was attempted in this set passed, and every one detected at least as many faults as NASA's. Sonnet's twelve files all reached full line and branch coverage. Qwen4's four passed with full coverage on two.
Put the two sets together and you get the chart at the top of this post: Sonnet at 20 of 22 files, Opus at 11 of 12, Qwen4 at 8 of 14.
Another angle: would the tests catch a real bug coming back?
Planted faults measure the general strength of a test file. A separate question is whether it would notice a specific, real bug returning. This is a narrower test, and it needs a caveat up front: the models wrote their tests against today's corrected code. So it shows whether a test file pins down correct behaviour firmly enough to flag an old bug. It does not show that a model would have found that bug in the first place.
To get real bugs I went through the full history of cFS and its apps. Take a commit that fixed a bug, un-apply it to today's code so the bug is back, rebuild, and see which tests notice. Most commits do not survive that treatment.
From 4,408 commits to 42 usable bugs
That left 42 real bug fixes across ten components. This round uses 13 of them, the ones in the twelve files of the second set. One HS fix touched three files and is scored once per file, so the charts below count 15 bugs.
Real bugs detected on all 12 files, out of 15
Real bugs detected on the four files Qwen4 ran, out of 7
Every real bug, and which test files detected it
| File | NASA | Sonnet 5.5 | Opus 5.5 | Qwen4 | Fixed | What the fix did |
|---|---|---|---|---|---|---|
| cf_codec.c | missed | caught | caught | too large | 2023-10 | Decoding segs using incorrect macro |
| cf_chunk.c | caught | caught | caught | too large | 2026-09 | Ignore empty ranges in CF_ChunkListAdd |
| cs_table_processing.c | missed | caught | not run | too large | 2026-03 | Adjust buffer termination |
| cs_compute.c | caught | caught | not run | too large | 2026-11 | App checksum handling after restart |
| ds_app.c | caught | caught | not run | caught | 2023-03 | Change EVS_Register failure from SendEvent to WriteToSysLog |
| ds_app.c | missed | missed | not run | missed | 2023-03 | Remove file age check bug |
| ds_app.c | caught | caught | not run | caught | 2024-07 | Prevent DS app exit on CFE_SB_NO_MESSAGE |
| ds_app.c | caught | caught | not run | caught | 2026-11 | Use RunStatus to guard fatal exit diagnostics |
| fm_app.c | caught | caught | not run | too large | 2023-03 | Change EVS_Register failure from SendEvent to WriteToSysLog |
| fm_cmds.c | missed | missed | not run | too large | 2026-07 | Add Null Terminator for FileName in FM_GetDirListFileCmd |
| hs_app_monitor.c | missed | caught | not run | caught | 2026-09 | Accept the highest valid action type (off-by-one check) |
| hs_event_monitor.c | missed | caught | not run | missed | 2026-09 | Accept the highest valid action type (off-by-one check) |
| hs_exec_monitor.c | missed | caught | not run | caught | 2026-09 | Use the right loop limit when refreshing execution counters |
| sc_atsrq.c | caught | caught | not run | too large | 2023-10 | Add extra range checking |
| sc_app.c | missed | caught | not run | too large | 2026-04 | Add check for CFE_SB_NO_MESSAGE |
This angle agrees with the first. Sonnet's tests detected every real bug that NASA's detect, plus six more, and the two they missed are ones NASA's also miss. Qwen4's detected two that NASA's miss, and missed one that Sonnet's caught.
Here is what a detection looks like. One HS fix changed a boundary check that had been rejecting the last valid action type:
/* hs_app_monitor.c, with the old bug put back */ + else if (ActionType >= (HS_AMTActType_LAST_NONMSG + HS_MAX_MSG_ACT_TYPES)) - else if (ActionType > (HS_AMTActType_LAST_NONMSG + HS_MAX_MSG_ACT_TYPES))
NASA's tests pass with the bug in place. Sonnet's fail, because one of its tests validates a table that uses the highest valid action type:
[ FAIL] hs_app_monitor_tests.c:537
HS_AppMon_ValidateTable(Table) (-1) == CFE_SUCCESS (0)
[ FAIL] hs_app_monitor_tests.c:540
context_CFE_EVS_SendEvent[0].EventID (51) == HS_AMTVal_INF_EID (50)
I re-ran seven of Sonnet's files in a separate build and read the failing checks for each detection. All were specific assertions like these. None was a crash or a timeout.
Why would NASA's tests miss real bugs?
This is less damning than it sounds. A bug that reached the repository is, almost by definition, one the tests of the day did not catch. The question here is whether today's tests would catch it if it came back, and for 8 of 15 they would not.
NASA's tests reach 100% line and branch coverage on all 12 files. So they execute the buggy lines, but do not check closely enough what those lines produce. The models' tests tend to assert more: exact return codes, event IDs, call counts.
Time and cost
Effort by run
| Run | Files passing | Passed first try | Full coverage | Time | API cost |
|---|---|---|---|---|---|
| Opus 5.5 · 10 HK/MD files | 10 of 10 | 3 | 9 | 63 min | $14.98 |
| Sonnet 5.5 · 10 HK/MD files | 9 of 10 | 2 | 8 | 40 min | $7.86 |
| Qwen4 · 10 HK/MD files | 5 of 10 | 0 | 3 | 115 min for 9 | none |
| Gemma 8-bit · 10 HK/MD files | 1 of 10 | 0 | 0 | 332 min | none |
| Gemma 6-bit · 10 HK/MD files | 0 of 10 | 0 | 0 | 75 min | none |
| Sonnet 5.5 · 12 real-bug files | 12 of 12 | 3 | 12 | 91 min | $17.15 |
| Opus 5.5 · 2 real-bug files | 2 of 2 | 1 | 2 | 13 min | $5.19 |
| Qwen4 · 4 real-bug files | 4 of 4 | 0 | 2 | 66 min | none |
The hosted models are fast and not free. The twelve files of the second set cost $17.15 with Sonnet, about $1.43 a file, because each prompt carries the app's headers and the replies run long. Opus cost about twice as much per file on the first set.
Qwen4 costs nothing per file and takes about four times as long on the files both finished. Its hard limit was memory. A cFS app's headers plus a long test file plus a repair round is a lot of context, and on a 64 GB machine that is where runs stopped. A machine with more memory should fit more of the files. I have not tested that.
Method and limits
- “Effective” is narrower than “good”. These measures say nothing about readability, maintainability, or whether NASA's engineers would accept the tests in review.
- NASA's tests are scored on something they may not have been written to maximise. Flight-software test standards emphasise coverage, and these tests achieve it fully.
- Planted faults are a proxy. They are small and mechanical, and some cannot be detected by any test.
- Single runs. Nothing was repeated, so small differences, such as one fault on one file, are noise.
- The models may have seen cFS. It is public, so NASA's tests may be in their training data.
- Uneven coverage of the second set. Opus ran 2 of the 12 files and Qwen4 ran 4.
- Real bugs: a small sample and my judgement. 15 bugs from 13 fixes, classified by my reading of the diffs. One bug either way moves the score by seven points.
- Local limits were mine to set. 131k context and 30 minutes per attempt. Early Gemma runs also had a 48,000-token reply cap, which seven attempts used up without producing code.
- Not yet run: the other 29 real bug fixes. Eleven in the OS abstraction layer are wired up and waiting. Eighteen in the core and elsewhere need a different harness.