【问题标题】:Debugging tests with delve使用 delve 调试测试
【发布时间】:2017-09-08 21:11:51
【问题描述】:

我正在使用 "go test -v" 来运行一堆单元测试。我想使用 delve 调试它们。当我尝试运行调试器时,我收到 “无法调试非主包” 错误。那么,如何使用 delve 调试器调试单元测试呢?

【问题讨论】:

    标签: debugging go delve


    【解决方案1】:

    使用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
    

    您也可以通过-test.run 选择要运行的测试(就像go test -run)。

    在内部,这与 Flimzy 的答案相同(它使用 go test -c 编译测试二进制文件),但更加精简,不会留下 .test 文件供您清理。

    【讨论】:

    【解决方案2】:

    我不熟悉 delve,但如果它可以在已编译的二进制文件上运行,只需使用 -c 标志编译您的测试:

        -c
            Compile the test binary to pkg.test but do not run it
            (where pkg is the last element of the package's import path).
            The file name can be changed with the -o flag.
    

    然后在输出上运行 delve。

    【讨论】:

    • 我让这个工作 dlv exec ./pkg.test -- -gocheck.f TestLoadImage 但输入“帮助”以获得命令列表。 (dlv) 步骤停止于:0x7fe7886b2090 => 1:没有可用的源命令失败:没有 PC 的源 0x7fe7886b2090 如何指示源代码的位置?
    猜你喜欢
    • 2018-12-02
    • 2016-08-18
    • 2018-09-13
    • 2019-04-11
    • 2019-03-09
    • 2017-09-20
    • 2016-12-27
    • 1970-01-01
    相关资源
    最近更新 更多