huffy

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2019 License: MIT Imports: 9 Imported by: 0

README

huffy

Go Report Card GoDoc

Huffy is a small library for unit testing in symbiosis with test data generators. It allows you to remember the test arguments that caused the fail test. At the next test run, these arguments will be used first.

Example

// calculate.go

package countchars

// Div must return result of division, rounded to biggest value
func Div(x, y int) int {
	return x / y
}

// calculate_test.go
package countchars

import (
	"math/rand"
	"testing"

	"github.com/ninedraft/huffy"
)

func TestDiv(test *testing.T) {
	type TestCase struct {
		X, Y     int
		Expected int
	}

	huffy.Tester{
		Generator: func(rnd *rand.Rand, id int) interface{} {
			var x = rnd.Intn(100) + 2
			var y = rnd.Intn(x-1) + 1
			var expected = x / y
			if x%y != 0 {
				expected++
			}
			return TestCase{
				X:        x,
				Y:        y,
				Expected: expected,
			}
		},
		Unit: func(test *testing.T, v interface{}) {
			var tc = v.(TestCase)
			var got = Div(tc.X, tc.Y)
			if tc.Expected != got {
				test.Fatalf("%d/%d: expected %d, got %d", tc.X, tc.Y, tc.Expected, got)
			}
		},
	}.R(test)
}
go test -timeout 30s github.com/ninedraft/huffy/examples/calculate -run ^(TestDiv)$ -race
--- FAIL: TestDiv (0.00s)
    huffy/examples/calculate/calculate_test.go:34: 44/25: expected 2, got 1
FAIL
FAIL	github.com/ninedraft/huffy/examples/calculate	0.041s
Error: Tests failed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator func(*rand.Rand, int) interface{}

Generator produces testdata for unit test

type TestCase

type TestCase struct {
	ID   int64       `json:"id"`
	Data interface{} `json:"data"`
}

TestCase is a storable data object, which plays role of container for generated test cases. Tester encoded

type Tester

type Tester struct {
	N         int
	TestFile  string
	Unit      Unit
	Generator Generator
	Rnd       *rand.Rand
}

Tester tries to to find generated test cases, which cause failed tests and write correpsonding test data to Tester.TestFile

func (Tester) R

func (tester Tester) R(test *testing.T)

R runs memorized tests, when runs at most N tests with generated test args. If tester.TestFile is "", then uses default value "testdata/TESTNAME.json" If Tester.N <= 0, then uses default value 400. If Tester.Rnd is nil, then uses random generator initialized with time.Now().UnixNano(). If any test fails, then Tester.R writes corresponding arg to tester.TestFile

type Unit

type Unit func(*testing.T, interface{})

Unit represents unit test. Must not call t.Parallel()!

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL