Debugging tests with delve

后端 未结 2 693
误落风尘
误落风尘 2021-02-07 15:53

I\'m using \"go test -v\" to run bunch of unit tests. I\'d like to debug them using delve. When I try to run debugger, I get an \"Can not debug non-main

2条回答
  •  没有蜡笔的小新
    2021-02-07 16:25

    Use dlv test:

    $ dlv test -- -test.v
    Type 'help' for list of commands.
    (dlv) continue
    === RUN   TestReadFileError
    --- PASS: TestReadFileError (0.00s)
    === RUN   TestReadFile
    --- PASS: TestReadFile (0.00s)
    [..]
    PASS
    Process 8014 has exited with status 0
    (dlv) quit
    Process 8014 has exited with status 0
    

    You can also pass -test.run to select tests to run (just like go test -run).

    Internally, this is the same as Flimzy's answer (it compiles the test binary with go test -c), but more streamlined and won't leave .test files for you to clean up.

提交回复
热议问题