mardi 16 mars 2021

Golang test fails but build succeeds

I am new to go and trying to write unit tests for my go-project.

The project directory structure is:

ProjectA
   | - pkg
   | - src
        | - ProjectA
              | - subdir1
              | - subdir2
                   |- subdir1.1
                   |- subdir2.1
                      | - x.go (pkg x)
                      | - x_test.go (pkg x)
              |- main.go
              |- go.mod
              |- go.sum

My go projects are arranged as follows:

/home/{user}/workspace
         | - MyGoProject1
         | - MyGoProject2
         | - ProjectA

go env:

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/{user}/.cache/go-build"
GOENV="/home/{user}/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/{user}/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build516319126=/tmp/go-build -gno-record-gcc-switches"

The problem :

  • go build ,go install works good and generates a binary I can use and works as expected.
  • I have written tests in x_test.go that work on exported functions in x.go. tried
    • $subdir2.1 go test -v
    • $subdir2.1 go test x.go x_test.go
    • $ProjectA(2nd layer) go test ./... -v

All of the above result in FAIL:

FAIL ProjectA/subdir2/subdir2.1 [build failed]

Note:

  • x.go has imports from different go files in subdir1,subdir1.1 etc...
  • my project has a go.mod and go.sum

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire