Skip to content

Debug Go tests

Stop inside a Go test.

Prerequisites

  • dlv on PATHcommand -v dlv
  • a Go module

The short way: the test under the cursor

:DebugTest

or

bugsaur gui --project . --test-at internal/orders/order_test.go:31

Bugsaur runs a test launch through dlv for the selected package or file. The adapter, environment and working directory come from the profile.

The buffer must be saved, and saved breakpoints are not picked up — a test run uses only the breakpoints of that request.

The long way: a package-test profile

Useful when you debug a package's whole test suite repeatedly.

[profiles.orders-test]
adapter = "dlv"
program = "internal/orders"
# `go test` runs the binary from the package directory — without this line
# `testdata/` is not found, because cwd defaults to the project root.
cwd = "internal/orders"

[profiles.orders-test.launch_arguments]
mode = "test"
bugsaur gui --project . --profile orders-test --break internal/orders/order.go:42

mode = \"test\" turns off Bugsaur's build

Normally Bugsaur compiles Go itself and hands Delve a finished binary. When a profile names mode in launch_arguments, it is driving the launch by hand: Delve builds the test binary itself, and our build would hand it the wrong file.

Run a single test from a profile

Pass the standard go test filter:

[profiles.orders-test.launch_arguments]
mode = "test"
args = ["-test.run", "TestCreateOrder$", "-test.v"]

The $ anchors the pattern; without it TestCreateOrder also matches TestCreateOrderFails.

When it does not work

Symptom Fix
testdata/ not found set cwd to the package directory
The whole suite runs, not one test add -test.run with an anchored pattern
Session fails with compiler output the tests do not compile; the text is in the Logs panel
Session never starts dlv not on PATHAdapter fails to start
Breakpoints on odd lines optimisation was not disabled in a custom build — keep -gcflags=all=-N -l
Thousands of threads those are goroutines — Threads