testparrot

package module
v0.0.0-...-227f600 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2022 License: MPL-2.0 Imports: 20 Imported by: 0

README

tests

go-testparrot 🦜

go-testparrot records and replies expected test values, so you don't have to hardcode complex test values.

About

Are you tired of hard coding values in tests? Do you copy paste failed test values like as monkey?

What if there would be a record button to record test values and reply them later, just like a parrot!

Features
  • Simple interface for record and reply values based on sequential or key value.
  • Generation of recorded values in readable go code.

Quick start

Create a package with some tests:
package example

import (
	"testing"

	"github.com/xtruder/go-testparrot"
)

func doSomething() string {
    return "value"
}

func TestSomething(t* testing.T) {
    value := doSomething()

    // When running in recording mode return value will equal
    // passed value and file with recordings will be generated.
    // When running without recording, values from recording file
    // will be replied.
    expected := testparrot.RecordNext(t, value)

    if value != expected {
        t.Errorf("doSomething() = %v; want %v", value, expected)
    }
}

func TestMain(m *testing.M) {
	testparrot.Run(m)
}

You must provide TestMain method that will run testparrot.Run of if you need additional steps after/before running tests, you can also use testparrot.BeforeTests and testparrot.AfterTests helper methods.

Record values

To record values run tests with recording enabled:

go test <package> -testparrot.record

This will record values and save them into <package>_recording_test.go file in same directory as tests.

You can also use go:generate by placing comment like:

//go:generate go test ./ -testparrot.record

in package under test and running go generate <package>

Run tests

Run tests like you woul ussually run them, but with recording disabled:

go test <package>

Developing go-testparrot

See CONTRIBUTING.md for best practices and instructions on setting up your development environment to work on Packer.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Must = must

export Must util method, so we can use it with some methods like json.Unmarshal

View Source
var Ptr = valToPtr

export Ptr util method to get pointer of a value using reflection

R defines a global test recorder

View Source
var Record = R.Record
View Source
var RecordNext = R.RecordNext

Functions

func AfterTests

func AfterTests(recorder *Recorder, recorderVar string)

AfterTests is method to use in TestMain after running tests

func BeforeTests

func BeforeTests(recorder *Recorder)

BeforeTests is method to use in TestMain before running tests

func Decode

func Decode(data interface{}, target interface{}) interface{}

func Run

func Run(m *testing.M)

Helper method to use in TestMain for running tests

Types

type GenOptions

type GenOptions struct {
	RecorderVar string
	Filter      func(map[string][]Recording) map[string][]Recording
}

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator generates golang code

func NewGenerator

func NewGenerator(pkgPath, pkgName string) *Generator

func (*Generator) Generate

func (g *Generator) Generate(recorder *Recorder, opts GenOptions, out io.Writer) error

func (*Generator) GenerateToFile

func (g *Generator) GenerateToFile(recorder *Recorder, opts GenOptions, filePath string) error

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

func NewRecorder

func NewRecorder() *Recorder

NewRecoder creates a new Recorder

func (*Recorder) EnableRecording

func (r *Recorder) EnableRecording(enable bool)

EnableRecording enables test recording

func (*Recorder) Load

func (r *Recorder) Load(name string, recordings []Recording)

Load method loads recording for a specific test name

func (*Recorder) Record

func (r *Recorder) Record(t *testing.T, key interface{}, value interface{}) interface{}

Recorder method records value under specified key. If recording is enabled Record returns provided value, otherwise it returns already recorded value.

func (*Recorder) RecordNext

func (r *Recorder) RecordNext(t *testing.T, value interface{}) interface{}

RecordNext method records next value in sequence. If recording is enabled Record returns provided value, otherwise it returns alreday recorded value.

func (*Recorder) RecordingEnabled

func (r *Recorder) RecordingEnabled() bool

RecordingEnabled returns whether recording is enabled

func (*Recorder) Reset

func (r *Recorder) Reset()

Reset method resets recorder

type Recording

type Recording struct {
	// Key defines record key
	Key interface{}

	// Value defines record value
	Value interface{}
}

Jump to

Keyboard shortcuts

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