Subtest debugging #122

Open
opened 2025-06-05 19:28:48 +03:00 by arodland · 2 comments
arodland commented 2025-06-05 19:28:48 +03:00 (Migrated from github.com)

Test functions can use t.Run() to create individually-addressable subtests. For example

func TestFruit(t *testing.T) {
    for _, fruit := range []string{"apple", "banana", "mango"} {
        t.Run(fruit, func(t *testing.T) {
            t.Logf("%s is delicious", fruit)
        })
    }
}

and we can use go test -run TestFruit/apple to run only the apple subtest. When debugging, this is very useful if we don't want to hit breakpoints in all of the other subtests.

We can actually get this behavior with nvim-dap-go by abusing debug_test's custom_config to override the args, like

local debug_subtest = function()
    local dap_go = require('dap-go')
    local ts = require('dap-go-ts')

    local subtest = vim.fn.input('Subtest name: ')
    local test = ts.closest_test()

    dap_go.debug_test({
        args = { '-test.run', '^' .. test.name .. '/' .. subtest .. '$' },
    })
end

but it would be a lot cleaner if it was integrated into nvim-dap-go directly.

Test functions can use `t.Run()` to create individually-addressable subtests. For example ```go func TestFruit(t *testing.T) { for _, fruit := range []string{"apple", "banana", "mango"} { t.Run(fruit, func(t *testing.T) { t.Logf("%s is delicious", fruit) }) } } ``` and we can use `go test -run TestFruit/apple` to run only the `apple` subtest. When debugging, this is very useful if we don't want to hit breakpoints in all of the other subtests. We can actually get this behavior with nvim-dap-go by abusing `debug_test`'s `custom_config` to override the `args`, like ```lua local debug_subtest = function() local dap_go = require('dap-go') local ts = require('dap-go-ts') local subtest = vim.fn.input('Subtest name: ') local test = ts.closest_test() dap_go.debug_test({ args = { '-test.run', '^' .. test.name .. '/' .. subtest .. '$' }, }) end ``` but it would be a lot cleaner if it was integrated into nvim-dap-go directly.
dennypenta commented 2025-08-11 00:46:13 +03:00 (Migrated from github.com)

did you consider using neotest golang for that purpose?
it has a dap strategy to run and contains a treesitter query to run an individual subtest.
or you mean this plugin should contain those treesitter queries?
https://github.com/fredrikaverpil/neotest-golang/blob/main/lua/neotest-golang/query.lua

did you consider using neotest golang for that purpose? it has a dap strategy to run and contains a treesitter query to run an individual subtest. or you mean this plugin should contain those treesitter queries? https://github.com/fredrikaverpil/neotest-golang/blob/main/lua/neotest-golang/query.lua
arodland commented 2025-08-11 02:57:46 +03:00 (Migrated from github.com)

@dennypenta no, I don't mean that. My tests with subtests are mostly table-tests, so a treesitter query looking for t.Run() with a string literal wouldn't get me anywhere (just like it wouldn't in the example I posted, because fruit isn't a literal).

My suggestion was to have nvim-dap-go prompt for the name of a subtest to run within the currently-selected test — again, just like the example in my issue, only without abusing debug_test's args.

@dennypenta no, I don't mean that. My tests with subtests are mostly table-tests, so a treesitter query looking for `t.Run()` with a string literal wouldn't get me anywhere (just like it wouldn't in the example I posted, because `fruit` isn't a literal). My suggestion was to have nvim-dap-go prompt for the name of a subtest to run within the currently-selected test — again, just like the example in my issue, only without abusing `debug_test`'s `args`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
NeonXP/nvim-dap-go#122
No description provided.