Home > Enterprise >  How to set path for go test in vscode
How to set path for go test in vscode

Time:02-02

I have following source tree:

/main.go
/templates/t.tmpl
/pkg/templates.go
/pkg/templates_test.go

/pkg/templates.go defines use of var t = template.Must(template.ParseFiles("templates/t.tmpl") which is needed for normal project operation, because binary is called from root folder.

And then when I am debugging test from templates_test.go from vscode Must fails, because go test current directory is pkg and not project root

How to make vscode with go test to use root project folder?

CodePudding user response:

Running the test from the package's source code directory is the default Go behavior.

https://pkg.go.dev/cmd/go#hdr-Testing_flags:

When 'go test' runs a test binary, it does so from within the corresponding package's source code directory. Depending on the test, it may be necessary to do the same when invoking a generated test binary directly.

To change the behavior, either use the relative path (e.g. go project's template package test), or change the desirable directory you want at the beginning of test program (e.g. os.Chdir from TestMain, etc).

  •  Tags:  
  • Related