Giter Club home page Giter Club logo

xorcare / golden Goto Github PK

View Code? Open in Web Editor NEW
30.0 2.0 4.0 268 KB

Package golden testing with golden files in Go. A golden file is the expected output of test, stored as a separate file rather than as a string literal inside the test code.

Home Page: https://pkg.go.dev/github.com/xorcare/golden

License: BSD 3-Clause "New" or "Revised" License

Go 94.22% Makefile 5.78%
go golden golden-tests go-library go-package golang testing tests testdata mit

golden's Introduction

Golden

codecov Go Report Card GoDoc

Package golden testing with golden files in Go. A golden file is the expected output of test, stored as a separate file rather than as a string literal inside the test code. So when the test is executed, it will read data from the file and compare it to the output produced by the functionality under test.

When writing unit tests, there comes a point when you need to check that the complex output of a function matches your expectations. This could be binary data (like an Image, JSON, HTML etc). Golden files are a way of ensuring your function output matches its .golden file. It’s a pattern used in the Go standard library.

One of the advantages of the gold files approach is that you can easily update the test data using the command line flag without copying the data into the text variables of go this is very convenient in case of significant changes in the behavior of the system but also requires attention to the changed test data and checking the correctness of the new golden results.

A special cli is provided in the package. The special flag -update is provided in the package for conveniently updating ethos files, for example, using the following command:

go test ./... -update

Golden files are placed in directory testdata this directory is ignored by the standard tools go, and it can accommodate a variety of data used in test or samples.

Installation

go get github.com/xorcare/golden

Examples

Inspiration

golden's People

Contributors

dependabot[bot] avatar xorcare avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

golden's Issues

Make a method for reading JSON input files

The method is needed to read JSON data with validation of the data.

var _ bytes.Buffer = golden.JSONRe(t)
// OR
var _ bytes.Buffer = golden.ReadJSON(t)

Original Russian text:

Сделать метод для чтения именно JSON входных файлов
Метод нужен для чтения данных JSON с проверкой валидности данных.

Add support for updating files via environment variable

When updating via a flag, errors may occur when the flag is not available in the abort of available flags in tests that do not use golden, therefore it is proposed to use an environment variable for these purposes.

func init() {
	update := func() bool {
		const flagname = "GOLDEN_UPDATE"
		flagval := os.Getenv(flagname)
		if flagval == "" {
			flagval = strconv.FormatBool(false)
		}

		b, err := strconv.ParseBool(flagval)
		if err != nil {
			panic(fmt.Sprintf("cannot parse flag %q, err: %v", flagname, err))
		}

		return b
	}

	tool.flag = flag.Bool("update", update(), "update test golden files")
}

Original Russian text:

Добавить поддержку для обновления файлов через переменную среды

При обновлении через флаг могут возникать ошибки отсутствия флага в наборе доступных флагов, в тех тестах в которых не используется golden по этому предлагается использовать для этих целей переменную окружения.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.