Go "panic: runtime error" during test - Fix in CI
A runtime fault - nil pointer dereference, index out of range, or invalid type assertion - panicked inside a test and crashed the whole test binary.
What this error means
A run fails with panic: runtime error: invalid memory address or nil pointer dereference and a stack trace. It often surfaces in CI when a fixture or dependency that exists locally is missing.
--- FAIL: TestParse (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0]
goroutine 6 [running]:
example.com/app.parse(0x0)Common causes
Nil value used without a guard
A pointer, map, or interface is nil at runtime and is dereferenced or indexed.
Missing fixture or environment in CI
A test depends on a file, env var, or service present locally but absent in CI, producing a nil where a value was expected.
How to fix it
Read the stack and guard the nil
- Follow the stack to the faulting line and add a nil/length check.
- Initialize maps and slices before use.
if cfg == nil {
t.Fatal("config not loaded")
}Provide the missing CI fixture
- Ensure required fixtures, env vars, or services exist in the CI job.
- run: cp testdata/config.example.yml testdata/config.ymlHow to prevent it
- Guard nil pointers, maps, and slice indexes in tested code.
- Make tests self-contained with checked-in fixtures.
- Fail fast with
t.Fatalwhen a precondition is missing.