MCPcopy Index your code
hub / github.com/approvals/go-approval-tests

github.com/approvals/go-approval-tests @v1.14.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.14.0 ↗ · + Follow
470 symbols 1,354 edges 58 files 82 documented · 17% 2 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

ApprovalTests.go

ApprovalTests for go

GoDoc Go Report Card Coverage Status Build and Test

Contents

Golden master Verification Library

ApprovalTests allows for easy testing of larger objects, strings and anything else that can be saved to a file (images, sounds, csv, etc...)

Examples

Basic string verification

func TestHelloWorld(t *testing.T) {
    t.Parallel()
    approvals.VerifyString(t, "Hello World!")
}

snippet source | anchor

Store approved files in testdata subfolder

Some people prefer to store their approved files in a subfolder "testdata" instead of in the same folder as the production code. To configure this, add a call to UseFolder to your TestMain:

func TestMain(m *testing.M) {
    approvals.UseFolder("testdata")
    os.Exit(m.Run())
}

snippet source | anchor

In Project

Note: ApprovalTests uses approvals to test itself. Therefore there are many examples in the code itself.

JSON

VerifyJSONBytes - Simple Formatting for easy comparison. Also uses the .json file extension

func TestVerifyJSON(t *testing.T) {
    t.Parallel()
    jsonb := []byte("{ \"foo\": \"bar\", \"age\": 42, \"bark\": \"woof\" }")
    approvals.VerifyJSONBytes(t, jsonb)
}

snippet source | anchor

Matches file: documentation_examples_test.TestVerifyJSON.approved.json

{
  "age": 42,
  "bark": "woof",
  "foo": "bar"
}

snippet source | anchor

Reporters

ApprovalTests becomes much more powerful with reporters. Reporters launch programs on failure to help you understand, fix and approve results.

You can make your own easily, here's an example You can also declare which one to use. Either at the

Method level

r := UseReporter(reporters.NewContinuousIntegrationReporter())
defer r.Close()

snippet source | anchor

Test Level

func TestMain(m *testing.M) {
    r := UseReporter(reporters.NewDiffReporter())
    defer r.Close()

    UseFolder("testdata")

    os.Exit(m.Run())
}

snippet source | anchor

Support and Documentation

Missing Documentation?

If there is documentation you wish existed, please add a page request to this issue.

Extension points exported contracts — how you extend this code

Reporter (Interface)
Reporter are called on failing approvals. [74 implementers]
reporters/reporter.go
Failable (Interface)
Failable is an interface wrapper around testing.T [1 implementers]
core/failable.go
ApprovalNamer (Interface)
(no doc) [1 implementers]
core/namer.go
Scrubber (FuncType)
(no doc)
scrubber.go
FileNameSanitizer (FuncType)
(no doc)
sanitize.go
ApprovalNamerCreator (FuncType)
(no doc)
core/namer.go

Core symbols most depended-on inside this repo

RegisterReporter
called by 69
reporters/environment_variable_reporter.go
launchProgram
called by 43
reporters/diff_reporter.go
NewFirstWorkingReporter
called by 36
reporters/reporter.go
Report
called by 27
reporters/reporter.go
Close
called by 16
approvals.go
VerifyString
called by 15
approvals.go
AssertEqual
called by 15
utils/testing_utils.go
ForFile
called by 13
options.go

Shape

Function 257
Method 118
Struct 89
FuncType 3
Interface 3

Languages

Go98%
Python2%

Modules by API surface

reporters/diff_reporters_generated.go169 symbols
approvals_test.go27 symbols
options.go19 symbols
approvals.go19 symbols
scrubber_test.go15 symbols
combination_approvals.go12 symbols
scripts/generate_diff_reporters.py11 symbols
date_scrubber_test.go11 symbols
date_scrubber.go10 symbols
console_output.go9 symbols
templated_custom_namer.go8 symbols
scrubber.go8 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add go-approval-tests \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page