approvaltests

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

README

ApprovalTests.go

ApprovalTests for go

GoDoc Go Report Card Coverage Status Build Status

Golden master Verification Library

ApprovalTests allows for easy testing of larger objects, strings and anything else that can be saved to a file (images, sounds, csv, etc...)

Examples

In Project

Note: ApprovalTests uses approvaltests to test itself. Therefore there are many examples in the code itself.

JSON

VerifyJSONBytes - Simple Formatting for easy comparison. Also uses the .json file extension

func TestVerifyJSON(t *testing.T) {
	jsonb := []byte("{ \"foo\": \"bar\", \"age\": 42, \"bark\": \"woof\" }")
	VerifyJSONBytes(t, jsonb)
}

Matches file: approvals_test.TestVerifyJSON.received.json

{
  "age": 42,
  "bark": "woof",
  "foo": "bar"
}

Reporters

ApprovalTests becomes much more powerful with reporters. Reporters launch programs on failure to help you understand, fix and approve results.

You can make your own easily, here's an example You can also declare which one to use. Either at the

Method level
r := UseReporter(reporters.NewIntelliJ())
defer r.Close()
Test Level
func TestMain(m *testing.M) {
	r := UseReporter(reporters.NewBeyondCompareReporter())
	defer r.Close()

	os.Exit(m.Run())
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// SkipThisCombination should be returned if you do not want to process a particular combination
	SkipThisCombination = "♬ SKIP THIS COMBINATION ♬"
)

Functions

func UseFrontLoadedReporter

func UseFrontLoadedReporter(reporter reporters.Reporter) io.Closer

UseFrontLoadedReporter configures reporters ahead of all other reporters to handle situations like CI. These reporters usually prevent reporting in scenarios that are headless.

func UseReporter

func UseReporter(reporter reporters.Reporter) io.Closer

UseReporter configures which reporter to use on failure. Add at the test or method level to configure your reporter.

The following examples shows how to use a reporter for all of your test cases in a package directory through go's setup feature.

func TestMain(m *testing.M) {
	r := UseReporter(reporters.NewBeyondCompareReporter())
     defer r.Close()

     os.Exit(m.Run())
}

func Verify

func Verify(t Failable, reader io.Reader) error

Verify Example:

Verify(t, strings.NewReader("Hello"))

func VerifyAll

func VerifyAll(t Failable, header string, collection interface{}, transform func(interface{}) string) error

VerifyAll Example:

VerifyAll(t, "uppercase", []string("dog", "cat"}, func(x interface{}) string { return strings.ToUpper(x.(string)) })

func VerifyAllCombinationsFor1

func VerifyAllCombinationsFor1(t Failable, header string, transform func(interface{}) string, collection1 interface{}) error

VerifyAllCombinationsFor1 Example:

VerifyAllCombinationsFor1(t, "uppercase", func(x interface{}) string { return strings.ToUpper(x.(string)) }, []string("dog", "cat"})

func VerifyAllCombinationsFor2

func VerifyAllCombinationsFor2(t Failable, header string, transform func(interface{}, interface{}) string, collection1 interface{}, collection2 interface{}) error

VerifyAllCombinationsFor2 calls the transform function with all combinations from collection 1 and collection 2. The resulting received file contains all inputs and the resulting outputs. The received file is then compared to the approved version. If the transform function returns SkipThisCombination the output of this combination won't be displayed inside the received file.

Example
letters := []string{"aaaaa", "bbbbb", "ccccc"}
numbers := []int{2, 3}

functionToTest := func(text interface{}, length interface{}) string {
	return text.(string)[:length.(int)]
}

approvaltests.VerifyAllCombinationsFor2(t, "substring", functionToTest, letters, numbers)
printFileContent("examples_test.ExampleVerifyAllCombinationsFor2.received.txt")
Output:

This produced the file examples_test.ExampleVerifyAllCombinationsFor2.received.txt
It will be compared against the examples_test.ExampleVerifyAllCombinationsFor2.approved.txt file
and contains the text:

substring

[aaaaa,2] => aa
[aaaaa,3] => aaa
[bbbbb,2] => bb
[bbbbb,3] => bbb
[ccccc,2] => cc
[ccccc,3] => ccc

func VerifyAllCombinationsFor3

func VerifyAllCombinationsFor3(
	t Failable,
	header string,
	transform func(p1, p2, p3 interface{}) string,
	collection1, collection2, collection3 interface{}) error

VerifyAllCombinationsFor3 is for combinations of 3.

func VerifyAllCombinationsFor4

func VerifyAllCombinationsFor4(
	t Failable,
	header string,
	transform func(p1, p2, p3, p4 interface{}) string,
	collection1, collection2, collection3, collection4 interface{}) error

VerifyAllCombinationsFor4 is for combinations of 4.

func VerifyAllCombinationsFor5

func VerifyAllCombinationsFor5(
	t Failable,
	header string,
	transform func(p1, p2, p3, p4, p5 interface{}) string,
	collection1, collection2, collection3, collection4, collection5 interface{}) error

VerifyAllCombinationsFor5 is for combinations of 5.

func VerifyAllCombinationsFor6

func VerifyAllCombinationsFor6(
	t Failable,
	header string,
	transform func(p1, p2, p3, p4, p5, p6 interface{}) string,
	collection1, collection2, collection3, collection4, collection5, collection6 interface{}) error

VerifyAllCombinationsFor6 is for combinations of 6.

func VerifyAllCombinationsFor7

func VerifyAllCombinationsFor7(
	t Failable,
	header string,
	transform func(p1, p2, p3, p4, p5, p6, p7 interface{}) string,
	collection1, collection2, collection3, collection4, collection5, collection6, collection7 interface{}) error

VerifyAllCombinationsFor7 is for combinations of 7.

func VerifyAllCombinationsFor8

func VerifyAllCombinationsFor8(
	t Failable,
	header string,
	transform func(p1, p2, p3, p4, p5, p6, p7, p8 interface{}) string,
	collection1, collection2, collection3, collection4, collection5, collection6, collection7, collection8 interface{}) error

VerifyAllCombinationsFor8 is for combinations of 8.

func VerifyAllCombinationsFor9

func VerifyAllCombinationsFor9(
	t Failable,
	header string,
	transform func(a, b, c, d, e, f, g, h, i interface{}) string,
	collection1,
	collection2,
	collection3,
	collection4,
	collection5,
	collection6,
	collection7,
	collection8,
	collection9 interface{}) error

VerifyAllCombinationsFor9 is for combinations of 9.

func VerifyArray

func VerifyArray(t Failable, array interface{}) error

VerifyArray Example:

VerifyArray(t, []string{"dog", "cat"})

func VerifyJSONBytes

func VerifyJSONBytes(t Failable, bs []byte) error

VerifyJSONBytes Example:

VerifyJSONBytes(t, []byte("{ \"Greeting\": \"Hello\" }"))

func VerifyJSONStruct

func VerifyJSONStruct(t Failable, obj interface{}) error

VerifyJSONStruct Example:

VerifyJSONStruct(t, json)

func VerifyMap

func VerifyMap(t Failable, m interface{}) error

VerifyMap Example:

VerifyMap(t, map[string][string] { "dog": "bark" })

func VerifyString

func VerifyString(t Failable, s string) error

VerifyString stores the passed string into the received file and confirms that it matches the approved local file. On failure, it will launch a reporter.

Example
approvaltests.VerifyString(t, "Hello World!")
printFileContent("examples_test.ExampleVerifyString.received.txt")
Output:

This produced the file examples_test.ExampleVerifyString.received.txt
It will be compared against the examples_test.ExampleVerifyString.approved.txt file
and contains the text:

Hello World!

func VerifyWithExtension

func VerifyWithExtension(t Failable, reader io.Reader, extWithDot string) error

VerifyWithExtension Example:

VerifyWithExtension(t, strings.NewReader("Hello"), ".txt")

func VerifyXMLBytes

func VerifyXMLBytes(t Failable, bs []byte) error

VerifyXMLBytes Example:

VerifyXMLBytes(t, []byte("<Test/>"))

func VerifyXMLStruct

func VerifyXMLStruct(t Failable, obj interface{}) error

VerifyXMLStruct Example:

VerifyXMLStruct(t, xml)

Types

type Failable

type Failable interface {
	Fail()
}

Failable is an interface wrapper around testing.T

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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