Skip to content

Commit

Permalink
Remove unnecessary error envelope in test output when custom asserts …
Browse files Browse the repository at this point in the history
…fail (#84)

Instead of

expected: <nil>(nil)
actual: *errors.errorString(&errors.errorString{s:"my error"})

we will now print

Received unexpected error: "my error"
  • Loading branch information
steinfletcher authored Jun 4, 2020
1 parent 5f8cab4 commit a5a25c3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
24 changes: 11 additions & 13 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,18 +399,16 @@ func (r *Request) Expect(t *testing.T) *Response {

// Response is the user defined expected response from the application under test
type Response struct {
status int
body string
headers map[string][]string
headersPresent []string
headersNotPresent []string
cookies []*Cookie
cookiesPresent []string
cookiesNotPresent []string
jsonPathExpression string
jsonPathAssert func(interface{})
apiTest *APITest
assert []Assert
status int
body string
headers map[string][]string
headersPresent []string
headersNotPresent []string
cookies []*Cookie
cookiesPresent []string
cookiesNotPresent []string
apiTest *APITest
assert []Assert
}

// Assert is a user defined custom assertion function
Expand Down Expand Up @@ -712,7 +710,7 @@ func (a *APITest) assertFunc(res *http.Response, req *http.Request) {
for _, assertFn := range a.response.assert {
err := assertFn(copyHttpResponse(res), copyHttpRequest(req))
if err != nil {
a.verifier.Equal(a.t, nil, err)
a.verifier.NoError(a.t, err)
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Verifier interface {
Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) bool
JSONEq(t *testing.T, expected string, actual string, msgAndArgs ...interface{}) bool
Fail(t *testing.T, failureMessage string, msgAndArgs ...interface{}) bool
NoError(t *testing.T, err error, msgAndArgs ...interface{}) bool
}

// testifyVerifier is a verifier that use https://github.com/stretchr/testify to perform assertions
Expand All @@ -33,6 +34,11 @@ func (a testifyVerifier) Fail(t *testing.T, failureMessage string, msgAndArgs ..
return assert.Fail(t, failureMessage, msgAndArgs...)
}

// NoError asserts that a function returned no error
func (a testifyVerifier) NoError(t *testing.T, err error, msgAndArgs ...interface{}) bool {
return assert.NoError(t, err, msgAndArgs...)
}

func newTestifyVerifier() Verifier {
return testifyVerifier{}
}
Expand All @@ -57,6 +63,11 @@ func (n NoopVerifier) Fail(t *testing.T, failureMessage string, msgAndArgs ...in
return true
}

// NoError asserts that a function returned no error
func (n NoopVerifier) NoError(t *testing.T, err error, msgAndArgs ...interface{}) bool {
return true
}

// IsSuccess is a convenience function to assert on a range of happy path status codes
var IsSuccess Assert = func(response *http.Response, request *http.Request) error {
if response.StatusCode >= 200 && response.StatusCode < 400 {
Expand Down
2 changes: 2 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
Expand Down Expand Up @@ -239,3 +240,4 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 1 addition & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
12 changes: 12 additions & 0 deletions mocks/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type MockVerifier struct {

FailFn func(t *testing.T, failureMessage string, msgAndArgs ...interface{}) bool
FailInvoked bool

NoErrorFn func(t *testing.T, err error, msgAndArgs ...interface{}) bool
NoErrorInvoked bool
}

func NewVerifier() MockVerifier {
Expand All @@ -30,6 +33,9 @@ func NewVerifier() MockVerifier {
FailFn: func(t *testing.T, failureMessage string, msgAndArgs ...interface{}) bool {
return true
},
NoErrorFn: func(t *testing.T, err error, msgAndArgs ...interface{}) bool {
return true
},
}
}

Expand All @@ -50,3 +56,9 @@ func (m MockVerifier) Fail(t *testing.T, failureMessage string, msgAndArgs ...in
m.FailInvoked = true
return m.FailFn(t, failureMessage, msgAndArgs)
}

// NoError asserts that a function returned no error
func (m MockVerifier) NoError(t *testing.T, err error, msgAndArgs ...interface{}) bool {
m.NoErrorInvoked = true
return m.NoErrorFn(t, err, msgAndArgs)
}

0 comments on commit a5a25c3

Please sign in to comment.