summaryrefslogtreecommitdiff
path: root/devdocs/go/testing%2Fquick%2Findex.html
blob: d7c9f316eeafc6e20eb6138ddf141690c3746d79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<h1> Package quick  </h1>     <ul id="short-nav">
<li><code>import "testing/quick"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package quick implements utility functions to help with black box testing. </p>
<p>The testing/quick package is frozen and is not accepting new features. </p>     <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#Check">func Check(f any, config *Config) error</a></li>
<li><a href="#CheckEqual">func CheckEqual(f, g any, config *Config) error</a></li>
<li><a href="#Value">func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool)</a></li>
<li><a href="#CheckEqualError">type CheckEqualError</a></li>
<li> <a href="#CheckEqualError.Error">func (s *CheckEqualError) Error() string</a>
</li>
<li><a href="#CheckError">type CheckError</a></li>
<li> <a href="#CheckError.Error">func (s *CheckError) Error() string</a>
</li>
<li><a href="#Config">type Config</a></li>
<li><a href="#Generator">type Generator</a></li>
<li><a href="#SetupError">type SetupError</a></li>
<li> <a href="#SetupError.Error">func (s SetupError) Error() string</a>
</li>
</ul> <h3>Package files</h3> <p>  <span>quick.go</span>  </p>   <h2 id="Check">func <span>Check</span>  </h2> <pre data-language="go">func Check(f any, config *Config) error</pre> <p>Check looks for an input to f, any function that returns bool, such that f returns false. It calls f repeatedly, with arbitrary values for each argument. If f returns false on a given input, Check returns that input as a *<a href="#CheckError">CheckError</a>. For example: </p>
<pre data-language="go">func TestOddMultipleOfThree(t *testing.T) {
	f := func(x int) bool {
		y := OddMultipleOfThree(x)
		return y%2 == 1 &amp;&amp; y%3 == 0
	}
	if err := quick.Check(f, nil); err != nil {
		t.Error(err)
	}
}
</pre> <h2 id="CheckEqual">func <span>CheckEqual</span>  </h2> <pre data-language="go">func CheckEqual(f, g any, config *Config) error</pre> <p>CheckEqual looks for an input on which f and g return different results. It calls f and g repeatedly with arbitrary values for each argument. If f and g return different answers, CheckEqual returns a *<a href="#CheckEqualError">CheckEqualError</a> describing the input and the outputs. </p>
<h2 id="Value">func <span>Value</span>  </h2> <pre data-language="go">func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool)</pre> <p>Value returns an arbitrary value of the given type. If the type implements the <a href="#Generator">Generator</a> interface, that will be used. Note: To create arbitrary values for structs, all the fields must be exported. </p>
<h2 id="CheckEqualError">type <span>CheckEqualError</span>  </h2> <p>A CheckEqualError is the result <a href="#CheckEqual">CheckEqual</a> finding an error. </p>
<pre data-language="go">type CheckEqualError struct {
    CheckError
    Out1 []any
    Out2 []any
}
</pre> <h3 id="CheckEqualError.Error">func (*CheckEqualError) <span>Error</span>  </h3> <pre data-language="go">func (s *CheckEqualError) Error() string</pre> <h2 id="CheckError">type <span>CheckError</span>  </h2> <p>A CheckError is the result of Check finding an error. </p>
<pre data-language="go">type CheckError struct {
    Count int
    In    []any
}
</pre> <h3 id="CheckError.Error">func (*CheckError) <span>Error</span>  </h3> <pre data-language="go">func (s *CheckError) Error() string</pre> <h2 id="Config">type <span>Config</span>  </h2> <p>A Config structure contains options for running a test. </p>
<pre data-language="go">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.
    // A count of zero implies the default, which is usually 100
    // but can be set by the -quickchecks flag.
    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)
}
</pre> <h2 id="Generator">type <span>Generator</span>  </h2> <p>A Generator can generate random values of its own type. </p>
<pre data-language="go">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
}</pre> <h2 id="SetupError">type <span>SetupError</span>  </h2> <p>A SetupError is the result of an error in the way that check is being used, independent of the functions being tested. </p>
<pre data-language="go">type SetupError string</pre> <h3 id="SetupError.Error">func (SetupError) <span>Error</span>  </h3> <pre data-language="go">func (s SetupError) Error() string</pre><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/testing/quick/" class="_attribution-link">http://golang.org/pkg/testing/quick/</a>
  </p>
</div>