rapid

A read-only mirror of https://git.kyotam.com/sigrid. It updates only from there.

NameLast commit messageLast commit date
.githubCI: test on Go 1.213 years ago
.editorconfigInitial commit7 years ago
.gitattributesci: use .gitattributes to work around Travis settings core.autocrlf=true on Windows7 years ago
.gitignorepersist: put failfiles under testdata/rapid/ subdirectories4 years ago
collections.goUse 'shorthand' in docs consistently3 years ago
collections_example_test.goYay generics4 years ago
collections_external_test.goYay generics4 years ago
collections_test.goYay generics4 years ago
combinators.gofeat: Add T.Context() (#77)1 year ago
combinators_example_test.goAdd ExampleMap()3 years ago
combinators_external_test.gofeat: Add T.Cleanup (#79)1 year ago
combinators_test.goRename Transform() back to Map()3 years ago
CONTRIBUTING.mdAdd CONTRIBUTING.md7 years ago
data.goDon't complain about bitstream usage for discarded groups3 years ago
data_test.goDefault to parallel tests7 years ago
doc.goReorder docs3 years ago
engine.godocs: add warning to SkipNow about mutation (#83)1 year ago
engine_fuzz_test.gofeat: Add T.Cleanup (#79)1 year ago
engine_test.gofeat: Add T.Cleanup (#79)1 year ago
example_function_test.goYay generics4 years ago
example_statemachine_test.goStateMachineActions, Repeat: rewrite to use Generator2 years ago
failure_external_test.goBetter state machine tests via t.Run()3 years ago
floats.goTweak float generators documentation a bit3 years ago
floats_external_test.goYay generics4 years ago
floats_test.goYay generics4 years ago
generator.gofeat: Add T.Context() (#77)1 year ago
generator_test.gofeat: Add T.Cleanup (#79)1 year ago
go.modYay generics4 years ago
integers.goYay generics4 years ago
integers_external_test.goYay generics4 years ago
LICENSEInitial commit7 years ago
make.goAdd note about limitation of Make for recursive types2 years ago
make_example_test.goFix Make[] for wrapper types3 years ago
persist.godiscard test log output2 years ago
persist_test.gofeat: Add T.Context() (#77)1 year ago
README.mdFix linter complaints in markdown3 years ago
regexp_external_test.goYay generics4 years ago
shrink.goAdjust number of checks performed based on the test deadline3 years ago
shrink_test.goRe-run fail files from testdata/rapid automatically3 years ago
statemachine.goStateMachineAction: GoString() instead of String()2 years ago
statemachine_test.goStateMachineActions, Repeat: rewrite to use Generator2 years ago
strings.goUse 'shorthand' in docs consistently3 years ago
strings_example_go121_test.goCI: test on Go 1.213 years ago
strings_example_test.goCI: test on Go 1.213 years ago
strings_external_test.goYay generics4 years ago
TODO.mdFix linter complaints in markdown3 years ago
utils.goYay generics4 years ago
utils_test.goYay generics4 years ago
vis.goAdd copyright notices7 years ago
vis_test.gofeat: Add T.Context() (#77)1 year ago
README.md

rapid PkgGoDev CI

Rapid is a Go library for property-based testing.

Rapid checks that properties you define hold for a large number of automatically generated test cases. If a failure is found, rapid automatically minimizes the failing test case before presenting it.

Features

  • Imperative Go API with type-safe data generation using generics
  • Data generation biased to explore "small" values and edge cases more thoroughly
  • Fully automatic minimization of failing test cases
  • Persistence and automatic re-running of minimized failing test cases
  • Support for state machine ("stateful" or "model-based") testing
  • No dependencies outside the Go standard library

Examples

Here is what a trivial test using rapid looks like (playground):

package rapid_test

import (
	"sort"
	"testing"

	"pgregory.net/rapid"
)

func TestSortStrings(t *testing.T) {
	rapid.Check(t, func(t *rapid.T) {
		s := rapid.SliceOf(rapid.String()).Draw(t, "s")
		sort.Strings(s)
		if !sort.StringsAreSorted(s) {
			t.Fatalf("unsorted after sort: %v", s)
		}
	})
}

More complete examples:

Comparison

Rapid aims to bring to Go the power and convenience Hypothesis brings to Python.

Compared to testing.F.Fuzz, rapid shines in generating complex structured data, including state machine tests, but lacks coverage-guided feedback and mutations. Note that with MakeFuzz, any rapid test can be used as a fuzz target for the standard fuzzer.

Compared to gopter, rapid provides a much simpler API (queue test in rapid vs gopter), is much smarter about data generation and is able to minimize failing test cases fully automatically, without any user code.

As for testing/quick, it lacks both convenient data generation facilities and any form of test case minimization, which are two main things to look for in a property-based testing library.

FAQ

What is property-based testing?

Suppose we've written arithmetic functions add, subtract and multiply and want to test them. Traditional testing approach is example-based — we come up with example inputs and outputs, and verify that the system behavior matches the examples:

func TestArithmetic_Example(t *testing.T) {
	t.Run("add", func(t *testing.T) {
		examples := [][3]int{
			{0, 0, 0},
			{0, 1, 1},
			{2, 2, 4},
			// ...
		}
		for _, e := range examples {
			if add(e[0], e[1]) != e[2] {
				t.Fatalf("add(%v, %v) != %v", e[0], e[1], e[2])
			}
		}
	})
	t.Run("subtract", func(t *testing.T) { /* ... */ })
	t.Run("multiply", func(t *testing.T) { /* ... */ })
}

In comparison, with property-based testing we define higher-level properties that should hold for arbitrary input. Each time we run a property-based test, properties are checked on a new set of pseudo-random data:

func TestArithmetic_Property(t *testing.T) {
	rapid.Check(t, func(t *rapid.T) {
		var (
			a = rapid.Int().Draw(t, "a")
			b = rapid.Int().Draw(t, "b")
			c = rapid.Int().Draw(t, "c")
		)
		if add(a, 0) != a {
			t.Fatalf("add() does not have 0 as identity")
		}
		if add(a, b) != add(b, a) {
			t.Fatalf("add() is not commutative")
		}
		if add(a, add(b, c)) != add(add(a, b), c) {
			t.Fatalf("add() is not associative")
		}
		if multiply(a, add(b, c)) != add(multiply(a, b), multiply(a, c)) {
			t.Fatalf("multiply() is not distributive over add()")
		}
		// ...
	})
}

Property-based tests are more powerful and concise than example-based ones — and are also much more fun to write. As an additional benefit, coming up with general properties of the system often improves the design of the system itself.

What properties should I test?

As you've seen from the examples above, it depends on the system you are testing. Usually a good place to start is to put yourself in the shoes of your user and ask what are the properties the user will rely on (often unknowingly or implicitly) when building on top of your system. That said, here are some broadly applicable and often encountered properties to keep in mind:

  • function does not panic on valid input data
  • behavior of two algorithms or data structures is identical
  • all variants of the decode(encode(x)) == x roundtrip

How does rapid work?

At its core, rapid does a fairly simple thing: generates pseudo-random data based on the specification you provide, and check properties that you define on the generated data.

Checking is easy: you simply write if statements and call something like t.Fatalf when things look wrong.

Generating is a bit more involved. When you construct a Generator, nothing happens: Generator is just a specification of how to Draw the data you want. When you call Draw, rapid will take some bytes from its internal random bitstream, use them to construct the value based on the Generator specification, and track how the random bytes used correspond to the value (and its subparts). This knowledge about the structure of the values being generated, as well as their relationship with the parts of the bitstream allows rapid to intelligently and automatically minify any failure found.

What about fuzzing?

Property-based testing focuses on quick feedback loop: checking the properties on a small but diverse set of pseudo-random inputs in a fractions of a second.

In comparison, fuzzing focuses on slow, often multi-day, brute force input generation that maximizes the coverage.

Both approaches are useful. Property-based tests are used alongside regular example-based tests during development, and fuzzing is used to search for edge cases and security vulnerabilities. With MakeFuzz, any rapid test can be used as a fuzz target.

Usage

Just run go test as usual, it will pick up also all rapid tests.

There are a number of optional flags to influence rapid behavior, run go test -args -h and look at the flags with the -rapid. prefix. You can then pass such flags as usual. For example:

go test -rapid.checks=10_000

Status

Rapid is stable: tests using rapid should continue to work with all future rapid releases with the same major version. Possible exceptions to this rule are API changes that replace the concrete type of parameter with an interface type, or other similar mostly non-breaking changes.

License

Rapid is licensed under the Mozilla Public License Version 2.0.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t