In The Go Programming Language, section 11.2.4, there is an example of of an external test accessing fmt.isSpace() via a declaration of IsSpace in fmt's export_test.go file. That seemed like the perfect solution, so that's what I did:
a/a.go:
package a
var x int
func Set(v int) {
x = v
}
a/a_test.go:
package a
import "testing"
func TestSet(t *testing.T) {
Set(42)
if x != 42 {
t.Errorf("x == %d (expected 42)", x)
}
}
func Get() int {
return x
}
(Running go test in a/ works fine.)
b/b.go:
package b
import "path/to/a"
func CallSet() {
a.Set(105)
}
b/b_test.go
package b
import (
"testing"
"path/to/a"
)
func TestCallSet(t *testing.T) {
CallSet()
if r := a.Get(); r != 105 {
t.Errorf("Get() == %d (expected 105)", r)
}
}
Unfortunately, when I run go test in b/, I get:
./b_test.go:11: undefined: a.Get
Trying to run both sets of test at the same time with go test ./... did not help.
After some considerable poking around I discover that "The *_test.go files are compiled into the package only when running go test for that package" (emphasis mine). (So, in other words I could access a.Get from an a_test external test package in a/, but not from any package outside of a/.)
Is there some other way I can allow tests from one package to access otherwise-internal data from another package, for integration testing purposes?
Aucun commentaire:
Enregistrer un commentaire