Skip to content
Latchkey

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

  1. Follow the stack to the faulting line and add a nil/length check.
  2. Initialize maps and slices before use.
Go
if cfg == nil {
	t.Fatal("config not loaded")
}

Provide the missing CI fixture

  1. Ensure required fixtures, env vars, or services exist in the CI job.
.github/workflows/ci.yml
- run: cp testdata/config.example.yml testdata/config.yml

How 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.Fatal when a precondition is missing.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →