goldtest

package module
v0.0.0-...-71a07d5 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2020 License: MIT Imports: 10 Imported by: 0

README

goldtest

GoDoc Go Report Card codecov coverage

goldtest is a simple package that improves experience of working with golden files. It allows you to assert any type of data with saved golden file. Currently there are three exported functions, I believe it should be enough for most cases. If you see room for improvement, please leave an issue or pull request.

What is Golden file?

Golden file is another way to store "expected output" of a function. One "golden file" stands for output from one test case. Usually those files are stored in separated files, in directory "testdata".

Example usage is in example directory. Also you can read a little bit more about package in THIS blog post

You can read more about it here:

Usage

You can update(create) golden files automatically by providing flag -update.

There are two main functions:

Assert() - good for smaller files, there is no need to have any field exported, works good with []byte.
AssertJSON() - works good for big structs (E.g tree), will give you more readible golden file. If you want to use it, all fields that should be tested should be exported.

Both works similar for build-in types (Eg. string, int, float32 etc.). Output is a bit different for maps, structs and bytes.

I suggest using JSON when you have to work with big data structures as it will be more readable.

Note: As you can see here: Assert AssertJSON, there is wrong output, when using AssertJSON(). This is how it is, so if you want use golden_file for bytes, consider using Assert()

Example can be found in example directory

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(t *testing.T, actual interface{}, fileName string)

Assert - allow testing any type of data. Function is able to load, update or create golden file and compare it with provided interface. Bytes for goldenfiles are created by converting interface to string, then to byte array.

func AssertJSON

func AssertJSON(t *testing.T, actual interface{}, fileName string)

AssertJSON - allow testing any type of data. Function is able to load, update or create golden file and compare it with provided interface. Bytes for goldenfiles are created by marshaling provided daya.

func GetGoldenFile

func GetGoldenFile(actual []byte, fileName string) ([]byte, error)

GetGoldenFile - return byte array. Allows you to use golden file content as you want. Also, if you pass flag "-update", golden file will be updated (or created).

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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