snapshot

package module
v0.0.1-beta.2 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2025 License: MIT Imports: 8 Imported by: 0

README

Go Reference golangci-lint Test and coverage

Snapshot Testing for Go

This is intended to be a simple drop in for adding image backed assertions to Go tests.

In short, it stores PNG files in a sub-directory and then compares the test inputs to the existing image.

Usage

package whatever_test

import (
    "image"
    "image/color"
    "image/draw"
    "testing"

    "github.com/jmhobbs/snapshot-assert"
)

func Test_Snapshot(t *testing.T) {
    // make a blue image, 50x50
    img := image.NewRGBA(image.Rect(0, 0, 50, 50))
    draw.Draw(
        img,
        img.Bounds(),
        image.NewUniform(color.RGBA{0, 0, 255, 255}),
        image.Point{},
        draw.Over,
    )

    // ensure it looks like the previous blue image
    snapshot.Assert(t, img)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(t testing.TB, actual image.Image)

func Fail

func Fail(t testing.TB, actual image.Image)

func Test

func Test(t testing.TB, actual image.Image) error

Types

type DiffFiles

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

func (DiffFiles) ActualPath

func (e DiffFiles) ActualPath() string

func (DiffFiles) CompositePath

func (e DiffFiles) CompositePath() string

type ErrBoundsMismatch

type ErrBoundsMismatch struct {
	DiffFiles
	// contains filtered or unexported fields
}

func (ErrBoundsMismatch) Error

func (e ErrBoundsMismatch) Error() string

type ErrPixelsDiffer

type ErrPixelsDiffer struct {
	DiffFiles
	// contains filtered or unexported fields
}

func (ErrPixelsDiffer) Error

func (e ErrPixelsDiffer) Error() string

type Option

type Option func(*Snapshots)

func WithDiffColor

func WithDiffColor(clr color.Color) Option

func WithStorageRoot

func WithStorageRoot(root string) Option

func WithTempDir

func WithTempDir(tmp string) Option

type Snapshots

type Snapshots struct {
	// contains filtered or unexported fields
}
var Default *Snapshots = New()

func New

func New(opts ...Option) *Snapshots

func (*Snapshots) Assert

func (s *Snapshots) Assert(t testing.TB, actual image.Image)

Assert that the provided image matches the snapshot. Cause a test error if not.

func (*Snapshots) AssertWithName

func (s *Snapshots) AssertWithName(t testing.TB, name string, actual image.Image)

Assert that the provided image matches the named snapshot. Cause a test error if not.

func (*Snapshots) Cleanup

func (s *Snapshots) Cleanup()

Remove all of the temporary files created by this instance.

func (*Snapshots) Fail

func (s *Snapshots) Fail(t testing.TB, actual image.Image)

Assert that the provided image matches the snapshot. Fail the test if not.

func (*Snapshots) FailWithName

func (s *Snapshots) FailWithName(t testing.TB, name string, actual image.Image)

Assert that the provided image matches the named snapshot. Fail the test if not.

func (*Snapshots) Test

func (s *Snapshots) Test(t testing.TB, actual image.Image) error

Test compares the actual image to the snapshot image. If the snapshot image does not exist, it will be created. If the images differ, an error is returned, either ErrBoundsMismatch or ErrPixelsDiffer. The error can be inspected to get the paths to the actual and composite diff images. A non-test failure will cause `testing.TB.Fatal()` to be called.

func (*Snapshots) TestWithName

func (s *Snapshots) TestWithName(t testing.TB, name string, actual image.Image) error

Run `Test` with a custom name.

Jump to

Keyboard shortcuts

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