Categorygithub.com/modern-go/test
modulepackage
0.0.0-20180301160529-68b5aafe843a
Repository: https://github.com/modern-go/test.git
Documentation: pkg.go.dev

# README

test

Sourcegraph GoDoc rcard License

Make go unit test more readable

  • C style assert, with clear error message
  • Diff object, if unequal, tells exactly where is the difference
  • JsonEqual to compare complex object graph

Assert

import (
	"context"
	. "github.com/modern-go/test"
	. "github.com/modern-go/test/must"
	"testing"
)

func Test(t *testing.T) {
	t.Run("one should not equal two", Case(func(ctx context.Context) {
		Assert(1 == 2)
	}))
}

If test failed, the error message will tell you where it failed and what has failed

example_test.go:12: failed: Assert(1 == 2)

Equal/NotEqual

the implementation of diff is copied from testify.

map1 := map[string]string{
"a": "b",
"c": "hello",
}
map2 := map[string]string{
"a": "b",
"c": "hi",
}
must.Equal(map1, map2)

the diff will be printed

Error: Not equal:
expected: map[string]string{"a":"b", "c":"hello"}
actual : map[string]string{"a":"b", "c":"hi"}

Diff:
--- Expected
+++ Actual
@@ -2,3 +2,3 @@
(string) (len=1) "a": (string) (len=1) "b",
- (string) (len=1) "c": (string) (len=5) "hello"
+ (string) (len=1) "c": (string) (len=2) "hi"
}
case_test.go:39: failed: must.Equal(map1, map2)

JsonEqual

check complex object graph using json

must.JsonEqual(`{"a":{"b":"c"}}`, map[string]interface{}{
    "a": map[string]interface{}{
        "b": "c",
    },
})

must v.s. should

  • must: when assertion failed, the test case will skip remaining assertions, skip to next test case
  • should: when assertion failed, the test case will continue check remaining assertions

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author