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.
go
--- 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.
Go
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.
.github/workflows/ci.yml
- 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.
Related guides
Go "test timed out after 10m0s" - Fix in CIFix Go "panic: test timed out after 10m0s" in CI - a test exceeded the default timeout, usually a hang or a s…
Go "DATA RACE" detected during test (-race) - Fix in CIFix Go "WARNING: DATA RACE" under go test -race in CI - concurrent unsynchronized access to shared state. Add…
Go "go test: exit status 2" - Fix in CIFix Go "go test ... exit status 2" in CI - a build or tooling failure, not a test assertion, ended the run. R…