• Stars
    star
    283
  • Rank 140,666 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 6 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple Go snapshot testing

Mascot
Build Status Coverage Status Go Report Card GoDoc

Incredibly simple Go snapshot testing: cupaloy takes a snapshot of your test output and compares it to a snapshot committed alongside your tests. If the values don't match then the test will be failed.

There's no need to manually manage snapshot files: just use the cupaloy.SnapshotT(t, value) function in your tests and cupaloy will automatically find the relevant snapshot file (based on the test name) and compare it with the given value.

Usage

Write a test

Firstly, write a test case generating some output and pass this output to cupaloy.SnapshotT:

func TestParsing(t *testing.T) {
    ast := ParseFile("test_input")

    // check that the result is the same as the last time the snapshot was updated
    // if the result has changed (e.g. because the behaviour of the parser has changed)
    // then the test will be failed with an error containing a diff of the changes
    cupaloy.SnapshotT(t, ast)
}

The first time this test is run, a snapshot will be automatically created (using the github.com/davecgh/go-spew package).

Update a snapshot

When the behaviour of your software changes causing the snapshot to change, this test will begin to fail with an error showing the difference between the old and new snapshots. Once you are happy that the new snapshot is correct (and hasn't just changed unexpectedly), you can save the new snapshot by setting the UPDATE_SNAPSHOTS environment and re-running your tests:

UPDATE_SNAPSHOTS=true go test ./...

This will fail all tests where the snapshot was updated (to stop you accidentally updating snapshots in CI) but your snapshot files will now have been updated to reflect the current output of your code.

Supported formats

Snapshots of test output are generated using the github.com/davecgh/go-spew package which uses reflection to deep pretty-print your test result and so will support almost all the basic types (from simple strings, slices, and maps to deeply nested structs) without issue. The only types whose contents cannot be fully pretty-printed are functions and channels.

The most important property of your test output is that it is deterministic: if your output contains timestamps or other fields which will change on every run, then cupaloy will detect this as a change and so fail the test.

Further Examples

Table driven tests

var testCases = map[string][]string{
    "TestCaseOne": []string{......},
    "AnotherTestCase": []string{......},
    ....
}

func TestCases(t *testing.T) {
    for testName, args := range testCases {
        t.Run(testName, func(t *testing.T) {
            result := functionUnderTest(args...)
            cupaloy.SnapshotT(t, result)
        })
    }
}

Changing output directory

func TestSubdirectory(t *testing.T) {
    result := someFunction()
    snapshotter := cupaloy.New(cupaloy.SnapshotSubdirectory("testdata"))
    err := snapshotter.Snapshot(result)
    if err != nil {
        t.Fatalf("error: %s", err)
    }
}

For further usage examples see basic_test.go and advanced_test.go in the examples/ directory which are both kept up to date and run on CI.

Debugging

Windows

It is important to note that git on Windows might be configured in a way that \n is replaced by \r\n during checkout (it is the case on GitHub actions). In such a case, the snapshot appears to be the same, but the test fails. Please ensure that git is configured correctly everywhere. There are multiple ways to do it, please check actions/checkout#135 and #73 for more details.

More Repositories

1

memviz

Visualize your Go data structures using graphviz
Go
1,294
star
2

grpc-tools

A suite of gRPC debugging tools. Like Fiddler/Charles but for gRPC.
Go
1,180
star
3

godoc-playground

Play with GoDoc syntax and preview your changes in realtime
Go
113
star
4

sigma-go

A Go implementation and parser for Sigma rules.
Go
67
star
5

abwhose

The simplest way to find how to report abusive domains
Go
54
star
6

nosleep.page

JavaScript
38
star
7

sigma-esf

Run Sigma detection rules on logs from the new MacOS EndpointSecurity Framework
Go
18
star
8

git-owners

Assign ownership to files and directories and ensure that owners review changes
Go
14
star
9

threathunting

Assorted, MIT licensed, threat hunting rules from @bradleyjkemp
12
star
10

submit-safebrowsing

The undocumented API for reporting sites to Safe Browsing
Go
10
star
11

sigmafmt

An opinionated formatter/linter for Sigma rules
Go
9
star
12

sigma-test

A test case runner for Sigma rules
Go
6
star
13

monkey

Tampering with unexported fields made easy
Go
6
star
14

hashcash-pow

Specification compliant, JavaScript Hashcash implementation
JavaScript
5
star
15

osquery-ja3

OSQuery extension to sniff TLS handshakes and extract JA3(S) signatures
Go
3
star
16

sigmadoc

A static site generator for @SigmaHQ rules
JavaScript
3
star
17

redactif

Go library for clearing marked fields in an arbitrary structure
Go
2
star
18

withtheflow

Lightweight Golang local workflow engine
Go
2
star
19

fakefilter-go

Go wrapper for https://github.com/7c/fakefilter
Go
1
star
20

sitehash

A small library for detecting changes to a domain
Go
1
star
21

git-metrics

Go
1
star
22

bradleyjkemp.github.io

HTML
1
star
23

fuss

A no-fuss way to fuzz functions with complex inputs
Go
1
star
24

goroutine-viz

Converts goroutine profiles into nice(ish) tree diagrams
Go
1
star