-
Notifications
You must be signed in to change notification settings - Fork 755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow running specific test case of a test table #2445
Comments
How would such a thing work? It seems like an impossible static analysis problem in all but the most trivial cases. |
Maybe name convention? Identify Footnotes
|
Behind the codelens, the extension simply invokes If users use the subtest, the extension currently has subtest support through the test explorer view. |
@ofabricio In your example, it is not possible to run a specific test case without changing func TestExample(t *testing.T) {
tt := []struct{
give int
then int
}{
{give: 1, then: 3},
{give: 2, then: 4},
{give: 3, then: 5},
}
for i, tc := range tt {
t.Run(fmt.Sprintf("Case %d", i+1), func(t *testing.T) {
v := tc.give + 2
assertEqual(t, v, tc.then, tc)
})
}
} If you know the name of the sub test you want to run, passing the right arguments to Statically discovering test cases is a complex problem. Finding a static subtest such as If we move test discovery out of the extension into gopls, it is more feasible to statically detect subtests. Though that will still be limited in scope as the halting problem makes it (literally) impossible to build a static analyzer that is guaranteed to run in finite time and find every subtest for an arbitrary input. @hyangah is moving test discovery to gopls an option? |
A simpler implementation would be to comment out all of the table except the line you want to execute. :) |
I didn't know the subtests appear in that view, as I don't use subtests often. I made a few tests with it and, although not so convenient, it kinda meet my needs tbh. So I don't think there is a need to implement my suggestion unless you think it worth it -- it still looks like a cool, handy feature :p What if instead of running a case, we add a "Extract test cases" option similar to "Extract function"? It would create a new test with just the selected cases. That would serve two purposes: I could debug one case separately by isolating it (as I currently manually do); or split a big test table (currently I have a test table with 600+ cases) |
Test manipulation utilities like "Extract test cases" sounds like a candidate for a separate extension. You might be able to use an approach similar to how I implemented a Go test explorer for the Test Explorer UI: https://gitlab.com/firelizzard/vscode-go-test-adapter/-/blob/5fe67440e8dfa2be14a840e084df165d59eb3c4e/src/model.ts#L111. |
I wouldn't say it's impossible, given that GoLand team has done it fairly reliably. Sure, we might not have the manpower, resources and the backend tooling - but it is doable. |
Is there any update on this issue? |
Realistically this requires static analysis, and the only reasonable way to do that is in Go (an analyzer for Go written in JavaScript is not reasonable), so it needs to be part of a separate service. gopls is the obvious choice for that and golang/go#59445 is a prerequisite. @tkgalk I'd believe GoLand can statically locate subtests (as in Or are you saying GoLand knows how to execute test cases when they're defined without |
GoLand can deal with table tests without individual https://www.jetbrains.com/go/guide/tips/run-test-in-table-test/ - here's more information about this. I think they are still relying on |
This is the key distinction. If you look at the screenshot in the issue description, there is no call to vscode-go already supports running subtests, and as you guessed it uses the I would like to work on this myself, but golang/go#59445 is a prerequisite at least for me since I have absolutely no interest in attempting that analysis in JavaScript. And as per golang/go#59445 (comment) it is probably not feasible for me as a contributor to handle the migration of test discovery to gopls, so I'm waiting for that task to be completed. |
In case this helps anyone, this is my current acceptable workflow while this is not a first-class feature:
And voilà, now you can debug sub-table-tests via button click. |
This is the same workaround that I use. But I want to set expectations - subtests will never be fully first-class. I want to improve the experience of dynamically discovered subtests and I want to add static subtest discover for common patterns but the halting problem means it's impossible to write a general algorithm that is 100% accurate for every possible case. |
something that's accurate for 80% case would be a win |
We can't possibly guarantee 80% or any percent. The best we can do is support a list of common, simple patterns. Without running a survey of the ecosystem (which I personally don't have time for) there's no way to know which patterns are used most. And complex patterns are somewhere between difficult and impossible to support. This should be easy to detect: func TestFoo(t *testing.T) {
cases := []struct{
Name string
Data any
}{
{Name: "foo", Data: 1},
{Name: "bar", Data: 2},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) { /* ... */ })
}
} On the other hand, this is impossible to statically analyze: func TestBar(t *testing.T) {
r, err := http.Get("https://example.com/api/user/1/posts")
if err != nil {
panic(err)
}
b, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
var posts []struct {
Title string
Data any
}
err = json.Unmarshal(b, &posts)
if err != nil {
panic(err)
}
for _, c := range posts {
t.Run(c.Title, func(t *testing.T) { /* ... */ })
}
} |
Change https://go.dev/cl/601015 mentions this issue: |
Change https://go.dev/cl/548675 mentions this issue: |
Implements test discovery. Tests are discovered as part of the type checking process, at the same time as method sets and xrefs, and cached. Does not implement the Modules command. Adds static detection of simple subtests. This provides a framework for static analysis of subtests but intentionally does not support more than the most trivial case in order to minimize the complexity of this CL. Fixes golang/go#59445. Updates golang/go#59445, golang/vscode-go#1602, golang/vscode-go#2445. Change-Id: Ief497977da09a1e07831e6c5f3b7d28d6874fd9f Reviewed-on: https://go-review.googlesource.com/c/tools/+/548675 Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Moving forward, test discovery will be handled by gopls. gopls does not yet have the ability to detect table-driven subtests, but it can identify subtests with hard-coded names, e.g. |
This is somewhat relevant: microsoft/vscode#126932 (comment) |
Change https://go.dev/cl/618216 mentions this issue: |
Related issues
I'd like to suggest a feature that allows running a specific test case in a test table.
When the cursor is in a test case line, a run button gutter could appear allowing you to run that specific case. Or a new option on top of the test function (see image below).
I have tests with a lot of cases and when I have to focus in one to debug it I have either to comment all the other cases or separate that one in a new test function.
The text was updated successfully, but these errors were encountered: