snapshot

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

goimagesnapshot

GoDoc Go Report Card

Snapshot testing for images in Go.

Usage

Using for a test

You can use Match function to test whether the given image is not changed as the previously generated snapshot image. If no snapshot image is found, a new snapshot is created and the test fails. By default, running a test creates __snapshot__ directory beside a test file and snapshot images are put in the directory.

import snap "github.com/midorimici/goimagesnapshot"

func TestSnapshot(t *testing.T) {
  t.Run("renders unchanged", func(t *testing.T) {
    snap.Match(t, img)
  })
}
Updating a snapshot

You can update existing snapshots by running tests with the UPDATE_SNAPSHOTS environment variable set.

# Updates snapshots with the same name, but does not delete obsolete files.
UPDATE_SNAPSHOTS=1 go test ./...

# Removes the snapshot directory at first (i.e. obsolete files are deleted) and then snapshots are regenerated.
UPDATE_SNAPSHOTS=2 go test ./...
Customization

You can specify some options to Match function or create a new snapshot matcher with custom options.

func TestSnapshot(t *testing.T) {
  // Change snapshot output directory
  m := snap.NewMatcher(snap.WithDirectory("testdata"))
  
  t.Run("renders unchanged", func(t *testing.T) {
    m.Match(
      t,
      img,
      snap.WithName("custom_file_name"),
      snap.WithThreshold(0.2),
      snap.WithOnlyPixelComparison(),
    )
  })
}

Documentation

Overview

Package snapshot provides utility routines for image snapshot testing.

With the package, you can ensure that an image look like the same as before: that there is no visual regression.

A PNG file is generated when running a test with Match function, and used to compare with images generated in subsequent tests.

import snap "github.com/midorimici/goimagesnapshot"

func TestSnapshot(t *testing.T) {
  t.Run("renders unchanged", func(t *testing.T) {
    snap.Match(t, img)
  })
}

To update snapshots, set UPDATE_SNAPSHOTS environment variable when running tests.

// Updates snapshots with the same name, but does not delete obsolete files.
UPDATE_SNAPSHOTS=1 go test ./...

// Removes the snapshot directory at first (i.e. obsolete files are deleted) and then snapshots are regenerated.
UPDATE_SNAPSHOTS=2 go test ./...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match

func Match(t *testing.T, i image.Image, opts ...option.SnapshotOption)

Match is matcher.Match with the default matcher options.

func WithDirectory

func WithDirectory(d string) option.MatcherOption

WithDirectory returns a snapshot matcher option which specifies the directory name where the output snapshot images are put.

The default value is "__snapshots__".

func WithName

func WithName(n string) option.SnapshotOption

WithName returns a snapshot option which specifies the output snapshot file name.

A file name uses the test name by default.

func WithOnlyPixelComparison

func WithOnlyPixelComparison() option.SnapshotOption

WithOnlyPixelComparison returns a snapshot option which specifies to compare only image pixels between two snapshots, which generally makes the test more strict.

By default, the matcher first compares the two image byte slices. If they differ, then their image pixels are tested one by one.

Specifying this option skips the first byte slice comparison.

func WithThreshold

func WithThreshold(t float64) option.SnapshotOption

WithThreshold returns a snapshot option which specifies a value which a test fails when the differences by percent in compared images exceeds. For example, when compared images have 0.11% of differences, a test fails with threshold of 0.1, whereas it passes with threshold of 0.2.

The default value is 0, i.e. two images must match perfectly.

Types

type Matcher

type Matcher interface {
	Match(t *testing.T, i image.Image, opts ...option.SnapshotOption)
}

func NewMatcher

func NewMatcher(opts ...option.MatcherOption) Matcher

NewMatcher returns a new matcher with matcher options.

Directories

Path Synopsis
examples
ebitengine Module
internal

Jump to

Keyboard shortcuts

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