mercredi 4 mars 2020

Create src files and test files in two separated directories in Golang

My Golang project files hierarchy be like:

.
├── src
│   └── sum.go
└── test
    └── sum_test.go

sum.go:

package sum

func Sum(a, b int) int {
    return a + b
}

sum_test.go:

import "testing"

func AssertEqual(t *testing.T, first interface{}, second interface{}) {
    if first != second {
        t.Errorf("Expected %+v, got: %+v", first, second)
    }
}

func TestSum(t *testing.T) {
    AssertEqual(t, Sum(2, 2), 4)
}

Once I run go test ./... in the root of the project (the directory above src and test), the output is:

# _/home/shayan/bench/gotest/test [_/home/shayan/bench/gotest/test.test]
test/sum_test.go:12:17: undefined: Sum
?       _/home/shayan/bench/gotest/src  [no test files]
FAIL    _/home/shayan/bench/gotest/test [build failed]
FAIL

I also have seen the following questions:

Golang tests in sub-directory

How to go test all testings in my project?

Could anyone correct me if it's possible to have src files separated from test files in two directories like above?

Aucun commentaire:

Enregistrer un commentaire