gosnap

package module
v0.0.0-...-61403c2 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2018 License: MIT Imports: 10 Imported by: 5

README

gosnap

Build Status GoDoc GitHub license Go Report Card GitHub issues

gosnap is a go package to perform snapshot testing.

Snapshot testing is a convenient way to test large outputs with ease, this package was initially created to test the go-gitlab-client CLI.

Install

go get github.com/plouc/gosnap

Usage

gosnap requires a context to run, from which you can create snapshots.

package whatever

import (
    "testing"
    "github.com/plouc/gosnap"
)

func TestSomething(t *testing.T) {
    // creates a new context
    // snapshots will be stored inside the `snapshots` directory
    ctx := gosnap.NewContext(t, "snapshots")

    // creates a new snapshot
    // it will be stored in `snapshots/my_first_snapshot.snap`
    s := ctx.NewSnapshot("my_first_snapshot")
    
    actual := DoSomethingWhichReturnsString()
    
    // this will load the snapshot content and check it matches `actual`
    s.AssertString(actual)
}

This will check that actual matches current snapshot (./snapshots/my_first_snapshot.snap) content.

The first time you run your tests, the snapshot will be created automatically, then if the current result does not match snapshot's content, you'll have to update it, you can add a command-line flag to the go test command to do so:

# will update all stale snapshots
go test -v ./... -args -update all

# will just update snapshot whose name is `my_snapshot`
go test -v ./... -args -update my_snapshot

For complete usage of gosnap, see the full package docs.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StringsDiff

func StringsDiff(a, b string) string

StringsDiff returns a pretty diff between given strings

Types

type Context

type Context struct {
	Dir                 string
	DirMode             os.FileMode
	FileMode            os.FileMode
	FileExtension       string
	AutoUpdate          bool
	AutoUpdateSnapshots []string
	// contains filtered or unexported fields
}

Context is used to isolate snapshot testing for given options

func NewContext

func NewContext(t *testing.T, d string) *Context

NewContext creates a new snapshot testing context

By default the `AutoUpdate` will be enabled according to `-update` or `-u` command-line flag, if it equals `all` `AutoUpdate` will be enabled.

If the update flag is set but doesn't equal `all`, then its value will be split using a comma, this allows to update specific snapshots only, if the snapshot's name matches one of the extracted value it will be updated, otherwise code has to be fixed :)

Note that even if it initially uses flag value, you can manually set `AutoUpdate` & `AutoUpdateSnapshots` manually.

func (*Context) Get

func (c *Context) Get(name string) *Snapshot

Get returns a snapshot by its name

func (*Context) Has

func (c *Context) Has(name string) bool

Has checks if a snapshot with the given name already exists

func (*Context) NewSnapshot

func (c *Context) NewSnapshot(name string) *Snapshot

NewSnapshot creates a new snapshot attached to context. If a snapshot with the same name already exists, test will fail.

type Snapshot

type Snapshot struct {
	Name string
	// contains filtered or unexported fields
}

Snapshot represents a snapshot file

func (*Snapshot) AssertString

func (s *Snapshot) AssertString(expected string)

AssertString test given string against stored content

func (*Snapshot) Content

func (s *Snapshot) Content() (string, error)

Content returns snapshot content and loads it if required

func (*Snapshot) File

func (s *Snapshot) File() (*os.File, error)

File returns snapshot file for reading

func (*Snapshot) FileName

func (s *Snapshot) FileName() string

FileName returns snapshot file name

func (*Snapshot) FilePath

func (s *Snapshot) FilePath() string

FilePath returns full snapshot path on disk

func (*Snapshot) Load

func (s *Snapshot) Load() error

Load loads snapshot file content

func (*Snapshot) Update

func (s *Snapshot) Update(c string) error

Update writes given content to disk and refresh content

Jump to

Keyboard shortcuts

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