首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

testing/quick

  • import "testing/quick"
  • 概观
  • 指数

概观

包快速实现实用功能,以帮助进行黑匣子测试。

测试/快速软件包已冻结,不接受新功能。

指数

  • func Check(f interface{}, config *Config) error
  • func CheckEqual(f, g interface{}, config *Config) error
  • func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool)
  • type CheckEqualError
  • func (s *CheckEqualError) Error() string
  • type CheckError
  • func (s *CheckError) Error() string
  • type Config
  • type Generator
  • type SetupError
  • func (s SetupError) Error() string

包文件

func CheckSource

代码语言:javascript
复制
func Check(f interface{}, config *Config) error

Check会查找f的输入,任何返回bool的函数都会返回false。它重复调用f,每个参数都有任意值。如果f在给定的输入上返回false,则Check将返回该输入为* CheckError。例如:

代码语言:javascript
复制
func TestOddMultipleOfThree(t *testing.T) {
	f := func(x int) bool {
		y := OddMultipleOfThree(x)
		return y%2 == 1 && y%3 == 0
	}
	if err := quick.Check(f, nil); err != nil {
		t.Error(err)
	}
}

func CheckEqualSource

代码语言:javascript
复制
func CheckEqual(f, g interface{}, config *Config) error

CheckEqual查找f和g返回不同结果的输入。它用每个参数的任意值重复调用f和g。如果f和g返回不同的答案,则CheckEqual将返回描述输入和输出的* CheckEqualError。

func ValueSource

代码语言:javascript
复制
func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool)

值返回给定类型的任意值。如果该类型实现了Generator接口,则将使用该接口。注意:要为结构创建任意值,必须导出所有字段。

type CheckEqualErrorSource

CheckEqualError是CheckEqual发现错误的结果。

代码语言:javascript
复制
type CheckEqualError struct {
        CheckError
        Out1 []interface{}
        Out2 []interface{}
}

func (*CheckEqualError) ErrorSource

代码语言:javascript
复制
func (s *CheckEqualError) Error() string

type CheckErrorSource

CheckError是检查发现错误的结果。

代码语言:javascript
复制
type CheckError struct {
        Count int
        In    []interface{}
}

func (*CheckError) ErrorSource

代码语言:javascript
复制
func (s *CheckError) Error() string

type ConfigSource

配置结构包含用于运行测试的选项。

代码语言:javascript
复制
type Config struct {
        // MaxCount sets the maximum number of iterations.
        // If zero, MaxCountScale is used.
        MaxCount int
        // MaxCountScale is a non-negative scale factor applied to the
        // default maximum.
        // If zero, the default is unchanged.
        MaxCountScale float64
        // Rand specifies a source of random numbers.
        // If nil, a default pseudo-random source will be used.
        Rand *rand.Rand
        // Values specifies a function to generate a slice of
        // arbitrary reflect.Values that are congruent with the
        // arguments to the function being tested.
        // If nil, the top-level Value function is used to generate them.
        Values func([]reflect.Value, *rand.Rand)
}

type GeneratorSource

发生器可以生成它自己类型的随机值。

代码语言:javascript
复制
type Generator interface {
        // Generate returns a random instance of the type on which it is a
        // method using the size as a size hint.
        Generate(rand *rand.Rand, size int) reflect.Value
}

type SetupErrorSource

SetupError是使用检查方式时出错的结果,与正在测试的函数无关。

代码语言:javascript
复制
type SetupError string

func (SetupError) ErrorSource

代码语言:javascript
复制
func (s SetupError) Error() string

扫码关注腾讯云开发者

领取腾讯云代金券