Testing 如何在Go包之间共享测试接口?
Go不会在不同包的测试文件之间共享代码,因此测试接口的定义不会自动重用。我们如何在实践中解决这个问题 使用Testing 如何在Go包之间共享测试接口?,testing,go,quickcheck,property-testing,Testing,Go,Quickcheck,Property Testing,Go不会在不同包的测试文件之间共享代码,因此测试接口的定义不会自动重用。我们如何在实践中解决这个问题 使用测试/quick的示例: foo/foo.go: package foo type Thing int const ( X Thing = iota Y Z ) package bar import ( "foo" ) type Box struct { Thing foo.Thing } package foo import ( "math/rand"
测试/quick
的示例:
foo/foo.go
:
package foo
type Thing int
const (
X Thing = iota
Y
Z
)
package bar
import (
"foo"
)
type Box struct {
Thing foo.Thing
}
package foo
import (
"math/rand"
"reflect"
"testing"
"testing/quick"
"time"
)
func (_ Thing) Generate(r *rand.Rand, sz int) reflect.Value {
return reflect.ValueOf(Thing(r.Intn(3)))
}
func TestGenThing(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
for i := 0; i < 5; i++ {
val, _ := quick.Value(reflect.TypeOf(Thing(0)), r)
tng, _ := val.Interface().(Thing)
t.Logf("%#v\n", tng)
}
}
bar/bar.go
:
package foo
type Thing int
const (
X Thing = iota
Y
Z
)
package bar
import (
"foo"
)
type Box struct {
Thing foo.Thing
}
package foo
import (
"math/rand"
"reflect"
"testing"
"testing/quick"
"time"
)
func (_ Thing) Generate(r *rand.Rand, sz int) reflect.Value {
return reflect.ValueOf(Thing(r.Intn(3)))
}
func TestGenThing(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
for i := 0; i < 5; i++ {
val, _ := quick.Value(reflect.TypeOf(Thing(0)), r)
tng, _ := val.Interface().(Thing)
t.Logf("%#v\n", tng)
}
}
我们想对foo
进行属性测试,所以我们定义了testing/quick.Generate
onThing
:
foo_test.go
:
package foo
type Thing int
const (
X Thing = iota
Y
Z
)
package bar
import (
"foo"
)
type Box struct {
Thing foo.Thing
}
package foo
import (
"math/rand"
"reflect"
"testing"
"testing/quick"
"time"
)
func (_ Thing) Generate(r *rand.Rand, sz int) reflect.Value {
return reflect.ValueOf(Thing(r.Intn(3)))
}
func TestGenThing(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
for i := 0; i < 5; i++ {
val, _ := quick.Value(reflect.TypeOf(Thing(0)), r)
tng, _ := val.Interface().(Thing)
t.Logf("%#v\n", tng)
}
}
让我们也来测试一下属性条
:
package bar
import (
"math/rand"
"reflect"
"testing"
"testing/quick"
"time"
"foo"
)
func (_ Box) Generate(r *rand.Rand, sz int) reflect.Value {
val, _ := quick.Value(reflect.TypeOf(foo.Thing(0)), r)
tng, _ := val.Interface().(foo.Thing)
return reflect.ValueOf(Box{tng})
}
func TestGenBox(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
for i := 0; i < 5; i++ {
val, _ := quick.Value(reflect.TypeOf(Box{}), r)
box, _ := val.Interface().(Box)
t.Logf("%#v\n", box)
}
}
有解决办法吗?人们在实践中如何使用
测试/quick
(或任何其他带有接口的测试库)?包之间共享的任何代码都必须位于非测试文件中。但这并不意味着它必须包含在任何最终构建中;您可以使用从正常生成中排除文件,并在运行测试时包含这些文件。例如,您可以将共享测试代码放在前缀为以下内容的文件中:
//+build testtools
package mypackage
(但未命名为_test.go)。生成时,此内容不会包含在生成中。当您进行测试时,您会使用以下内容:
go test -tags "testtools" ./...
这将在构建中包括受约束的文件,从而使共享代码可供测试使用