Golang: tests and working directory

As a workaround, I compiled the test and execute the test from the current directory.

go test -c && ./<mypackage>.test

Or, if you want a generic command that you can use, you can rename the test file with -o option.

go test -c -o xyz.test && ./xyz.test


I do not believe this is possible. I have not been able to find documentation stating this explicitly, but I believe go test always uses the package directory (containing the go source files) as the working directory.


You may be able to use the Caller to get the path to the current test source file, like this:

package sample

import (
    "testing"
    "runtime"
    "fmt"
)

func TestGetFilename(t *testing.T) {
    _, filename, _, _ := runtime.Caller(0)
    t.Logf("Current test filename: %s", filename)
}