Is it possible to debug_test a specific test table? #75
Labels
No labels
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
NeonXP/nvim-dap-go#75
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Say I have this test structure:
When I run debug test it will run all four tests in this table. Is it possible to make
require('dap-go').debug_test()debug only a specific test in this array of tests?to add to this, I believe that it would be hard to implement since there are a lot of variances you can have for a test table setup, but maybe we could provide an input suggestion for the test name?
for example, when we run
debug_test()with the cursor inside a string, it could prompt us to choose if we want to just run the test function (current behavior) or the test with the name that is inside that string. Would that be possible?You are right that implementing this logic wouldn't be trivial. There is no pattern to follow as test tables in go is not really standardized by the language. As a workaround, what I do is adding a
skipattribute in every test case and calltest.SkipNow()if it is set to true. Not ideal but gets the job done.is it viable to implement passing test names to debug_test? maybe with that we could open the possibility to run a test with a specific name inside a function, and the user figures out how to pass this name to the function
I managed to do it the following way:
I added a new configuration:
This will run just the test that's name is determined by the function called in
args. For the sake of completeness here is how I implemented it (it relies on the fact that all of my table tests have a name as astringas the first case-property:Acknowledgements: all of that code is cobbled together from various plugins determining test- and test-case names (partially from this plugin, partially from
ray-x/go.nvim, partially fromcrispgm/nvim-go).For some more inspiration the neotest-go adapter for the neotest plugin also managed to implement, very successfully, running individual test in a table driven test, both for slices and maps of tests, see this issue for an example: https://github.com/nvim-neotest/neotest-go/issues/43.
I've been using
neotestwith the golang adapter for quite some time, and it's been working flawlessly for me.I managed to get it working for normal test running, but this fails when trying to debug a test, because it seems that neotest-go doesn't support nvim-dap
My comment above was just to show that another plugin was able to isolate the subtests inside a table test, so in theory, we should be able to use the same treesitter query to do the same with
nvim-dap-go.As I'm not sure when if ever this will be implemented in
nvim-dap-go, I'd like to document here the 2 methods I use to debug individual subtests inside a table driven test setup.(PS: Leoluz's workaround works, but it's too many code changes, especially if you have lots of test cases)
Let's take Bruno's example code and add a little bit to it:
With
neotestI'm able to run individual tests inside a table (regardless of whether it's a slice or a map containing the tests).When it comes to debugging the tests individually, I too, have a "workaround". In particular, I use 2 methods.
The first method is to copy the test name, and set a conditional breakpoint when the test name matches the string literal.
So, if I want to specifically debug the third test, I'll place my conditional breakpoint on line
result := SomeFunction(test.a, test.b)and the condition will look liketest.name == "third test". The debugger will run and stop on that line exactly when the name of the test matches what I want.This isn't much work, since I just move to the line with the test name do a
vi"to select the test name inside quotes, then"+yto copy it to the clipboard, then move to the line I want to place my conditional breakpoint in, hit a keymap that will show a popup where I can just type the condition above and paste the name of the test. It's very fast.NOTE: DO NOT set the conditional breakpoint on the line with the
t.Run()method call. Delve seems to get confused and tries to evaluate the variabletestinside the code in the standard library fort.Runwhich obviously doesn't exist (I'm not talking about the closure here, I'm talking about the actual code that is part of t.Run. But I'm not entirely sure what's the problem there, I just know there is defo a problem!).This works like a charm, however, there is a second method I occasionally use, which is to literally put a temporary if statement in my code and then set a normal breakpoint inside of it. I don't tend to use this to select a specific test name, but rather when I have more complex conditional logic. This is only temporary code that I make sure to delete once I'm done with the debugging session.
With the code above, that would look like:
As I said, this second method is particularly powerful when you have complex conditional logic that may even require you calling other methods in your code. This is nice because delve has limitations on what you can use as an expression.
However, as powerful as this can be, it's not really necessary when all you want is to select a specific subtest in a table test.
In that case, just use method 1.
Hope that helps 🙌
PS: Leo thanks a bunch for creating this plugin and maintaining it. I rely on it on a daily basis!! 🦸
@gustavooferreira tks a lot for your feedback. Now I am curious to see how
neotestachieved this. :)This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.