test

package module
v0.0.0-...-68b5aaf Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2018 License: Apache-2.0 Imports: 9 Imported by: 1

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Case

func Case(testCase func(ctx context.Context)) func(t *testing.T)

func CurrentT

func CurrentT() *testing.T

func ExtractFailedLines

func ExtractFailedLines(file string, line int) string

func Helper

func Helper()

func Skip

func Skip(args ...interface{})

func Skipf

func Skipf(format string, args ...interface{})

Types

This section is empty.

Directories

Path Synopsis
testify
assert
Package assert provides a set of comprehensive testing tools for use with the normal Go testing system.
Package assert provides a set of comprehensive testing tools for use with the normal Go testing system.

Jump to

Keyboard shortcuts

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