Golang тестирование ошибок

I often use this simple function to check errors:

// ErrorContains checks if the error message in out contains the text in
// want.
//
// This is safe when out is nil. Use an empty string for want if you want to
// test that err is nil.
func ErrorContains(out error, want string) bool {
    if out == nil {
        return want == ""
    }
    if want == "" {
        return false
    }
    return strings.Contains(out.Error(), want)
}

Example usage:

if !ErrorContains(err, "unexpected banana") {
    t.Errorf("unexpected error: %v", err)
}

if !ErrorContains(err, "") {
    t.Errorf("unexpected error: %v", err)
}

I find this especially useful in table-driven tests, as I can do something like:

tests := struct{
    want    string
    wantErr string
}{
    {
        "some output",
        "",  // No error expected
    },
    {
        "",
        "out of coconuts",
    }
}

It avoids mucking about with nil, errors.New(), and the like.

I use string.Contains() instead of checking the full error as that’s more robust. I just want to know if this is roughly the error I’m expecting (and not a completely unrelated one). I rarely check for the full error message, but use keywords instead (e.g. «unexpected end», «not enough», etc.)


This function is part of the github.com/teamwork/test package (I am the chief author of that), but I often just copy/paste it if I use only this function and none of the others in that package.

While it is a known fact that programmers never make mistakes, it is still a good idea to humor the users by checking for errors at critical points in your programs.

—Robert D. Schneider, “Optimizing INFORMIX Applications”

How should we test errors in Go? In other words, if part of the behaviour of your public API is to return error values in certain situations, how do you test that? And what implications does that have for the way we construct, handle, and pass around errors within our Golang programs?

This is the first of a three-part series of tutorials on Golang error testing:

  1. Testing errors in Go
  2. Comparing Go error values
  3. Error wrapping in Go

Ignoring errors is a mistake

First, let’s talk about unexpected errors in Go tests. It’s common for Go functions to return an error value as part of their results. It’s especially common for functions to return “something and error”.

In this case, the function returns two things: the actual result (the piece of data it was supposed to produce) and an accompanying value of type error. The error value indicates whether or not the function was successful. If the error is nil, everything is fine and the result data is valid. Otherwise, the result data should be ignored, and the error value will explain what went wrong.

For example:

func CreateUser(u User) (UserID, error) {

How do we test a function like this? Let’s look at a few of the wrong ways to do it first.

One really wrong way would be something like this:

func TestCreateUser(t *testing.T) {
    want := 1
    got, _ := CreateUser("some valid user")
    if want != got {
        t.Errorf("want user ID %d, got %d", want, got)
    }
}

We know from its signature that CreateUser returns an error as part of its API, but we ignore this result value completely in the test, by assigning it to the blank identifier (_).

And you can see why this is a bad idea, can’t you? It’s another example of the happy-path fixation. If there were some bug that caused CreateUser to return an error when it shouldn’t, would this test detect it? Nope.

But there’s a more subtle problem, too. What if there were a bug in the test? We appear to be testing the “valid input” behaviour, but what if we mistakenly passed invalid input instead? What would we see when we ran the test?

Wait, what? We can stare at the code for CreateUser as long as we like, and we won’t see the problem that’s causing this, because it isn’t there.

The problem is not that want doesn’t equal got, although that happens to be true. The problem is that we shouldn’t have even looked at got, because there was an error. By returning a zero user ID and an error, CreateUser is trying to tell us that something’s gone wrong, but we’re not listening.

This is something that often trips up people new to Go. In some languages, functions can signal an unhandled error condition using an exception, for example, which would cause a test like this to fail automatically. This isn’t the case in Go, where we signal that got is invalid by also returning a non-nil error.

That puts the burden of checking the error on the caller: in this case, that’s the test. So, as we can see, it’s easy to make a mistake where an unchecked error results in the code proceeding as though got were valid when it’s not.

We don’t want to write tests for our tests (where would it end?), but we can write our tests in a defensive way, by always checking errors. At least then if we do make a mistake, the resulting failure will give us a clue about what it was.

Ignoring error values in a test, or indeed anywhere in a Go program, is a pretty common mistake. It might save us a second or two now, but it’ll cost us (or our successors) a lot of puzzlement later. Let’s not store up any more trouble for the future than we have to.

A great way to add value and robustness to any existing Go test suite is to go through it looking for places where errors are ignored using _ (static analysers such as errcheck can find such places for you). Remove the blank identifier and assign the error result to the err variable, then check it and fail the test with t.Fatal if necessary.

Unexpected errors should stop the test

How exactly should we fail the test if there’s an unexpected error? One idea is to call t.Error, but is that good enough?

func TestCreateUser(t *testing.T) {
    ...
    got, err := CreateUser(testUser)
    if err != nil {
        t.Error(err)
    }
    ... // more testing here
}

No, because t.Error, though it marks the test as failed, also continues the test. That’s not the right thing to do here. We need to stop the test right away. Why?

If err is not nil, then we don’t have a valid result, so we shouldn’t go on to test anything about it. Indeed, even looking at the value of got could be dangerous.

Consider, for example, some function store.Open:

func Open(path string) (*Store, error) {
    ... // load data from 'path' if possible
    ... // but this could fail:
    if err != nil {
        return nil, err
    }
    return &Store{
        Data: data,
    }, nil
}

It’s conventional among Gophers that functions like this should return a nil pointer in the error case. So any code that tries to dereference that pointer will panic when it’s nil:

func TestOpen(t *testing.T) {
    s, err := store.Open("testdata/store.bin")
    if err != nil {
        t.Error(err)
    }
    for _, v := range s.Data { // no! panics if s is nil
        ...
    }
}

Suppose store.bin doesn’t exist, so Open can’t open it, and returns an error. The test detects this, and calls t.Error to report it, but then continues.

Now we’re going to try to range over s.Data. But that won’t work when s is nil, as it will be if there was an error. Dereferencing the s pointer will cause the test to panic, confusingly.

This isn’t a total disaster, because the test will still fail, so we’ll at least know that something’s wrong. But it’s also important to make sure that a test fails for the right reasons. Otherwise, it’s hard to know what it’s actually telling us about the problem.

The problem, it turns out, is not in Open at all. It’s in our test, because we should never have touched the s pointer once we knew it was nil.

NIGEL: Don’t touch it!
MARTY: I wasn’t going to touch it, I was just pointing at it.
NIGEL: Well, don’t point, even.

—“This is Spinal Tap”

Instead, the test should call t.Fatal to bail out immediately. Or, in this case, since we’d like to include some formatted data in the message, t.Fatalf:

func TestOpen(t *testing.T) {
    s, err := store.open("testdata/store.bin")
    if err != nil {
        t.Fatalf("unexpected error opening test store: %v", err)
    }
    ... // now we can safely use 's'
}

t.Fatal isn’t just for unexpected errors from the system under test. We should also use it to report any problems we encounter when setting up the context for the test, such as our want value. After all, if we can’t set up the necessary preconditions, we need to stop the test before it proceeds any further.

Suppose, for example, that we want to compare a function’s result against the contents of some pre-prepared golden file. And suppose that file doesn’t happen to exist for some reason, or we just got the name wrong.

For example, suppose we spell it godlen.txt instead of golden.txt. In that event, we’d want to see a test failure that tells us what’s wrong, and we wouldn’t want to continue with the test. So, again, we can use t.Fatalf:

// Can you spot the deliberate mistake?
want, err := os.ReadFile("testdata/godlen.txt")
if err != nil {
    t.Fatalf("unexpected error reading golden file: %v", err)
}

This is another kind of potentially confusing test bug. We can detect it by being disciplined about our error checking, and using t.Fatal to short-circuit the test.

Error behaviour is part of your API

So that’s how we deal with unexpected errors, but what about expected errors? After all, if errors are part of our public API, which they usually are, then we need to test them. Arguably, the way the system behaves when things go wrong is even more important than what it does when things go right.

Here’s why. If the user calls our function and it just works, that’s great: they’ll never think any more about it. But if it doesn’t work for some reason, now the user has a problem and they need our help.

The user needs to know, not only that this operation didn’t work in general, but more importantly, what specifically didn’t work about it, and why not. We can go further, and also suggest what they might do to solve the problem, for example. This kind of care and forethought in writing helpful, informative error messages adds a lot of value to code.

Judging by some of the error messages I’ve encountered, it’s clear that the programmer didn’t really expect anyone to ever read them. Sometimes they betray a certain amount of inner angst:

I think, on the contrary, your error messages are the most important part of your API. When things go wrong, we can’t expect users to be delighted, but we can at least put a little effort into making the resulting error messages as helpful as possible.

So if we’re to treat errors as a critical part of the system’s behaviour, how can we test them? What should we test, and when?

At a minimum, our tests should check two things. One, that the system produces an error when it should. Two, that it doesn’t produce one when it shouldn’t.

That already sounds like two tests, doesn’t it: one for the “want error” case, and another for “want no error”. Let’s apply this idea to testing some function format.Data, for example. We’ll write both “want error” and “want no error” tests, and see what the typical pattern looks like for each.

Let’s write “want error” first, because it’s easier:

func TestFormatData_ErrorsOnInvalidInput(t *testing.T) {
    _, err := format.Data(invalidInput)
    if err == nil {
        t.Error("want error for invalid input")
    }
}

(Listing format/1)

And that’s it! No need to go on and compare want with got, for example. We already know that got will be empty or invalid, and indeed we mustn’t even point at it.

That’s why we ignore the other return value using the blank identifier _. We don’t plan to check that value, because the only thing this test cares about is that err is not nil under these circumstances.

For the “want no error” case, though, it makes sense to check that the function also produces the right result, provided that we bail out if it produces an error instead:

func TestFormatData_IsCorrectForValidInput(t *testing.T) {
    want := validInputFormatted
    got, err := format.Data(validInput)
    if err != nil {
        t.Fatal(err)
    }
    if want != got {
        t.Error(cmp.Diff(want, got))
    }
}

(Listing format/1)

Note that even though we’re not expecting an error in this case, we still receive it and check it, and we use t.Fatal to short-circuit the test if it’s not nil. That’s because what this test cares about is not only that the function doesn’t return an error, but also that it returns the right result. Both of those things are essential to the function’s public behaviour in the “valid input” case.

Checking the error here catches bugs in the function’s behaviour, which is important, but that’s not the only reason to do it. It’s always possible that the test could be wrong, too. A bug in the system is bad enough, but a bug in the test is much worse. It means you can no longer reliably detect bugs in the system.


In Part 2, we’ll talk about how to produce certain errors in order to test them, and also about how to compare error values in Go. See you soon!

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

Example Usage ¶

The following is a complete example using assert in a standard test function:

import (
  "testing"
  "github.com/stretchr/testify/assert"
)

func TestSomething(t *testing.T) {

  var a string = "Hello"
  var b string = "Hello"

  assert.Equal(t, a, b, "The two words should be the same.")

}

if you assert many times, use the format below:

import (
  "testing"
  "github.com/stretchr/testify/assert"
)

func TestSomething(t *testing.T) {
  assert := assert.New(t)

  var a string = "Hello"
  var b string = "Hello"

  assert.Equal(a, b, "The two words should be the same.")
}

Assertions ¶

Assertions allow you to easily write test code, and are global funcs in the `assert` package.
All assertion functions take, as the first argument, the `*testing.T` object provided by the
testing framework. This allows the assertion funcs to write the failings and other details to
the correct place.

Every assertion function also takes an optional string message as the final argument,
allowing custom error messages to be appended to the message the assertion method outputs.

  • Variables
  • func CallerInfo() []string
  • func Condition(t TestingT, comp Comparison, msgAndArgs …interface{}) bool
  • func Conditionf(t TestingT, comp Comparison, msg string, args …interface{}) bool
  • func Contains(t TestingT, s, contains interface{}, msgAndArgs …interface{}) bool
  • func Containsf(t TestingT, s interface{}, contains interface{}, msg string, …) bool
  • func DirExists(t TestingT, path string, msgAndArgs …interface{}) bool
  • func DirExistsf(t TestingT, path string, msg string, args …interface{}) bool
  • func ElementsMatch(t TestingT, listA, listB interface{}, msgAndArgs …interface{}) (ok bool)
  • func ElementsMatchf(t TestingT, listA interface{}, listB interface{}, msg string, …) bool
  • func Empty(t TestingT, object interface{}, msgAndArgs …interface{}) bool
  • func Emptyf(t TestingT, object interface{}, msg string, args …interface{}) bool
  • func Equal(t TestingT, expected, actual interface{}, msgAndArgs …interface{}) bool
  • func EqualError(t TestingT, theError error, errString string, msgAndArgs …interface{}) bool
  • func EqualErrorf(t TestingT, theError error, errString string, msg string, args …interface{}) bool
  • func EqualExportedValues(t TestingT, expected, actual interface{}, msgAndArgs …interface{}) bool
  • func EqualExportedValuesf(t TestingT, expected interface{}, actual interface{}, msg string, …) bool
  • func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs …interface{}) bool
  • func EqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, …) bool
  • func Equalf(t TestingT, expected interface{}, actual interface{}, msg string, …) bool
  • func Error(t TestingT, err error, msgAndArgs …interface{}) bool
  • func ErrorAs(t TestingT, err error, target interface{}, msgAndArgs …interface{}) bool
  • func ErrorAsf(t TestingT, err error, target interface{}, msg string, args …interface{}) bool
  • func ErrorContains(t TestingT, theError error, contains string, msgAndArgs …interface{}) bool
  • func ErrorContainsf(t TestingT, theError error, contains string, msg string, args …interface{}) bool
  • func ErrorIs(t TestingT, err, target error, msgAndArgs …interface{}) bool
  • func ErrorIsf(t TestingT, err error, target error, msg string, args …interface{}) bool
  • func Errorf(t TestingT, err error, msg string, args …interface{}) bool
  • func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, …) bool
  • func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time.Duration, …) bool
  • func EventuallyWithTf(t TestingT, condition func(collect *CollectT), waitFor time.Duration, …) bool
  • func Eventuallyf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, …) bool
  • func Exactly(t TestingT, expected, actual interface{}, msgAndArgs …interface{}) bool
  • func Exactlyf(t TestingT, expected interface{}, actual interface{}, msg string, …) bool
  • func Fail(t TestingT, failureMessage string, msgAndArgs …interface{}) bool
  • func FailNow(t TestingT, failureMessage string, msgAndArgs …interface{}) bool
  • func FailNowf(t TestingT, failureMessage string, msg string, args …interface{}) bool
  • func Failf(t TestingT, failureMessage string, msg string, args …interface{}) bool
  • func False(t TestingT, value bool, msgAndArgs …interface{}) bool
  • func Falsef(t TestingT, value bool, msg string, args …interface{}) bool
  • func FileExists(t TestingT, path string, msgAndArgs …interface{}) bool
  • func FileExistsf(t TestingT, path string, msg string, args …interface{}) bool
  • func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs …interface{}) bool
  • func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs …interface{}) bool
  • func GreaterOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args …interface{}) bool
  • func Greaterf(t TestingT, e1 interface{}, e2 interface{}, msg string, args …interface{}) bool
  • func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string
  • func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, …) bool
  • func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url string, …) bool
  • func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, …) bool
  • func HTTPBodyNotContainsf(t TestingT, handler http.HandlerFunc, method string, url string, …) bool
  • func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, …) bool
  • func HTTPErrorf(t TestingT, handler http.HandlerFunc, method string, url string, …) bool
  • func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, …) bool
  • func HTTPRedirectf(t TestingT, handler http.HandlerFunc, method string, url string, …) bool
  • func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, …) bool
  • func HTTPStatusCodef(t TestingT, handler http.HandlerFunc, method string, url string, …) bool
  • func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, …) bool
  • func HTTPSuccessf(t TestingT, handler http.HandlerFunc, method string, url string, …) bool
  • func Implements(t TestingT, interfaceObject interface{}, object interface{}, …) bool
  • func Implementsf(t TestingT, interfaceObject interface{}, object interface{}, msg string, …) bool
  • func InDelta(t TestingT, expected, actual interface{}, delta float64, …) bool
  • func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, …) bool
  • func InDeltaMapValuesf(t TestingT, expected interface{}, actual interface{}, delta float64, …) bool
  • func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, …) bool
  • func InDeltaSlicef(t TestingT, expected interface{}, actual interface{}, delta float64, …) bool
  • func InDeltaf(t TestingT, expected interface{}, actual interface{}, delta float64, …) bool
  • func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, …) bool
  • func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, …) bool
  • func InEpsilonSlicef(t TestingT, expected interface{}, actual interface{}, epsilon float64, …) bool
  • func InEpsilonf(t TestingT, expected interface{}, actual interface{}, epsilon float64, …) bool
  • func IsDecreasing(t TestingT, object interface{}, msgAndArgs …interface{}) bool
  • func IsDecreasingf(t TestingT, object interface{}, msg string, args …interface{}) bool
  • func IsIncreasing(t TestingT, object interface{}, msgAndArgs …interface{}) bool
  • func IsIncreasingf(t TestingT, object interface{}, msg string, args …interface{}) bool
  • func IsNonDecreasing(t TestingT, object interface{}, msgAndArgs …interface{}) bool
  • func IsNonDecreasingf(t TestingT, object interface{}, msg string, args …interface{}) bool
  • func IsNonIncreasing(t TestingT, object interface{}, msgAndArgs …interface{}) bool
  • func IsNonIncreasingf(t TestingT, object interface{}, msg string, args …interface{}) bool
  • func IsType(t TestingT, expectedType interface{}, object interface{}, …) bool
  • func IsTypef(t TestingT, expectedType interface{}, object interface{}, msg string, …) bool
  • func JSONEq(t TestingT, expected string, actual string, msgAndArgs …interface{}) bool
  • func JSONEqf(t TestingT, expected string, actual string, msg string, args …interface{}) bool
  • func Len(t TestingT, object interface{}, length int, msgAndArgs …interface{}) bool
  • func Lenf(t TestingT, object interface{}, length int, msg string, args …interface{}) bool
  • func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs …interface{}) bool
  • func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs …interface{}) bool
  • func LessOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args …interface{}) bool
  • func Lessf(t TestingT, e1 interface{}, e2 interface{}, msg string, args …interface{}) bool
  • func Negative(t TestingT, e interface{}, msgAndArgs …interface{}) bool
  • func Negativef(t TestingT, e interface{}, msg string, args …interface{}) bool
  • func Never(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, …) bool
  • func Neverf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, …) bool
  • func Nil(t TestingT, object interface{}, msgAndArgs …interface{}) bool
  • func Nilf(t TestingT, object interface{}, msg string, args …interface{}) bool
  • func NoDirExists(t TestingT, path string, msgAndArgs …interface{}) bool
  • func NoDirExistsf(t TestingT, path string, msg string, args …interface{}) bool
  • func NoError(t TestingT, err error, msgAndArgs …interface{}) bool
  • func NoErrorf(t TestingT, err error, msg string, args …interface{}) bool
  • func NoFileExists(t TestingT, path string, msgAndArgs …interface{}) bool
  • func NoFileExistsf(t TestingT, path string, msg string, args …interface{}) bool
  • func NotContains(t TestingT, s, contains interface{}, msgAndArgs …interface{}) bool
  • func NotContainsf(t TestingT, s interface{}, contains interface{}, msg string, …) bool
  • func NotEmpty(t TestingT, object interface{}, msgAndArgs …interface{}) bool
  • func NotEmptyf(t TestingT, object interface{}, msg string, args …interface{}) bool
  • func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs …interface{}) bool
  • func NotEqualValues(t TestingT, expected, actual interface{}, msgAndArgs …interface{}) bool
  • func NotEqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, …) bool
  • func NotEqualf(t TestingT, expected interface{}, actual interface{}, msg string, …) bool
  • func NotErrorIs(t TestingT, err, target error, msgAndArgs …interface{}) bool
  • func NotErrorIsf(t TestingT, err error, target error, msg string, args …interface{}) bool
  • func NotNil(t TestingT, object interface{}, msgAndArgs …interface{}) bool
  • func NotNilf(t TestingT, object interface{}, msg string, args …interface{}) bool
  • func NotPanics(t TestingT, f PanicTestFunc, msgAndArgs …interface{}) bool
  • func NotPanicsf(t TestingT, f PanicTestFunc, msg string, args …interface{}) bool
  • func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs …interface{}) bool
  • func NotRegexpf(t TestingT, rx interface{}, str interface{}, msg string, args …interface{}) bool
  • func NotSame(t TestingT, expected, actual interface{}, msgAndArgs …interface{}) bool
  • func NotSamef(t TestingT, expected interface{}, actual interface{}, msg string, …) bool
  • func NotSubset(t TestingT, list, subset interface{}, msgAndArgs …interface{}) (ok bool)
  • func NotSubsetf(t TestingT, list interface{}, subset interface{}, msg string, …) bool
  • func NotZero(t TestingT, i interface{}, msgAndArgs …interface{}) bool
  • func NotZerof(t TestingT, i interface{}, msg string, args …interface{}) bool
  • func ObjectsAreEqual(expected, actual interface{}) bool
  • func ObjectsAreEqualValues(expected, actual interface{}) bool
  • func ObjectsExportedFieldsAreEqual(expected, actual interface{}) bool
  • func Panics(t TestingT, f PanicTestFunc, msgAndArgs …interface{}) bool
  • func PanicsWithError(t TestingT, errString string, f PanicTestFunc, msgAndArgs …interface{}) bool
  • func PanicsWithErrorf(t TestingT, errString string, f PanicTestFunc, msg string, args …interface{}) bool
  • func PanicsWithValue(t TestingT, expected interface{}, f PanicTestFunc, msgAndArgs …interface{}) bool
  • func PanicsWithValuef(t TestingT, expected interface{}, f PanicTestFunc, msg string, …) bool
  • func Panicsf(t TestingT, f PanicTestFunc, msg string, args …interface{}) bool
  • func Positive(t TestingT, e interface{}, msgAndArgs …interface{}) bool
  • func Positivef(t TestingT, e interface{}, msg string, args …interface{}) bool
  • func Regexp(t TestingT, rx interface{}, str interface{}, msgAndArgs …interface{}) bool
  • func Regexpf(t TestingT, rx interface{}, str interface{}, msg string, args …interface{}) bool
  • func Same(t TestingT, expected, actual interface{}, msgAndArgs …interface{}) bool
  • func Samef(t TestingT, expected interface{}, actual interface{}, msg string, …) bool
  • func Subset(t TestingT, list, subset interface{}, msgAndArgs …interface{}) (ok bool)
  • func Subsetf(t TestingT, list interface{}, subset interface{}, msg string, …) bool
  • func True(t TestingT, value bool, msgAndArgs …interface{}) bool
  • func Truef(t TestingT, value bool, msg string, args …interface{}) bool
  • func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, …) bool
  • func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta time.Duration, …) bool
  • func WithinRange(t TestingT, actual, start, end time.Time, msgAndArgs …interface{}) bool
  • func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, msg string, …) bool
  • func YAMLEq(t TestingT, expected string, actual string, msgAndArgs …interface{}) bool
  • func YAMLEqf(t TestingT, expected string, actual string, msg string, args …interface{}) bool
  • func Zero(t TestingT, i interface{}, msgAndArgs …interface{}) bool
  • func Zerof(t TestingT, i interface{}, msg string, args …interface{}) bool
  • type Assertions
    • func New(t TestingT) *Assertions
    • func (a *Assertions) Condition(comp Comparison, msgAndArgs …interface{}) bool
    • func (a *Assertions) Conditionf(comp Comparison, msg string, args …interface{}) bool
    • func (a *Assertions) Contains(s interface{}, contains interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Containsf(s interface{}, contains interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) DirExists(path string, msgAndArgs …interface{}) bool
    • func (a *Assertions) DirExistsf(path string, msg string, args …interface{}) bool
    • func (a *Assertions) ElementsMatch(listA interface{}, listB interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) ElementsMatchf(listA interface{}, listB interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Empty(object interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Emptyf(object interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Equal(expected interface{}, actual interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) EqualError(theError error, errString string, msgAndArgs …interface{}) bool
    • func (a *Assertions) EqualErrorf(theError error, errString string, msg string, args …interface{}) bool
    • func (a *Assertions) EqualExportedValues(expected interface{}, actual interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) EqualExportedValuesf(expected interface{}, actual interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) EqualValues(expected interface{}, actual interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) EqualValuesf(expected interface{}, actual interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Equalf(expected interface{}, actual interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Error(err error, msgAndArgs …interface{}) bool
    • func (a *Assertions) ErrorAs(err error, target interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) ErrorAsf(err error, target interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) ErrorContains(theError error, contains string, msgAndArgs …interface{}) bool
    • func (a *Assertions) ErrorContainsf(theError error, contains string, msg string, args …interface{}) bool
    • func (a *Assertions) ErrorIs(err error, target error, msgAndArgs …interface{}) bool
    • func (a *Assertions) ErrorIsf(err error, target error, msg string, args …interface{}) bool
    • func (a *Assertions) Errorf(err error, msg string, args …interface{}) bool
    • func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, …) bool
    • func (a *Assertions) EventuallyWithT(condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, …) bool
    • func (a *Assertions) EventuallyWithTf(condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, …) bool
    • func (a *Assertions) Eventuallyf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, …) bool
    • func (a *Assertions) Exactly(expected interface{}, actual interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Exactlyf(expected interface{}, actual interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Fail(failureMessage string, msgAndArgs …interface{}) bool
    • func (a *Assertions) FailNow(failureMessage string, msgAndArgs …interface{}) bool
    • func (a *Assertions) FailNowf(failureMessage string, msg string, args …interface{}) bool
    • func (a *Assertions) Failf(failureMessage string, msg string, args …interface{}) bool
    • func (a *Assertions) False(value bool, msgAndArgs …interface{}) bool
    • func (a *Assertions) Falsef(value bool, msg string, args …interface{}) bool
    • func (a *Assertions) FileExists(path string, msgAndArgs …interface{}) bool
    • func (a *Assertions) FileExistsf(path string, msg string, args …interface{}) bool
    • func (a *Assertions) Greater(e1 interface{}, e2 interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) GreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) GreaterOrEqualf(e1 interface{}, e2 interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Greaterf(e1 interface{}, e2 interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPErrorf(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPRedirectf(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPStatusCode(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPStatusCodef(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) HTTPSuccessf(handler http.HandlerFunc, method string, url string, values url.Values, …) bool
    • func (a *Assertions) Implements(interfaceObject interface{}, object interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Implementsf(interfaceObject interface{}, object interface{}, msg string, …) bool
    • func (a *Assertions) InDelta(expected interface{}, actual interface{}, delta float64, …) bool
    • func (a *Assertions) InDeltaMapValues(expected interface{}, actual interface{}, delta float64, …) bool
    • func (a *Assertions) InDeltaMapValuesf(expected interface{}, actual interface{}, delta float64, msg string, …) bool
    • func (a *Assertions) InDeltaSlice(expected interface{}, actual interface{}, delta float64, …) bool
    • func (a *Assertions) InDeltaSlicef(expected interface{}, actual interface{}, delta float64, msg string, …) bool
    • func (a *Assertions) InDeltaf(expected interface{}, actual interface{}, delta float64, msg string, …) bool
    • func (a *Assertions) InEpsilon(expected interface{}, actual interface{}, epsilon float64, …) bool
    • func (a *Assertions) InEpsilonSlice(expected interface{}, actual interface{}, epsilon float64, …) bool
    • func (a *Assertions) InEpsilonSlicef(expected interface{}, actual interface{}, epsilon float64, msg string, …) bool
    • func (a *Assertions) InEpsilonf(expected interface{}, actual interface{}, epsilon float64, msg string, …) bool
    • func (a *Assertions) IsDecreasing(object interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) IsDecreasingf(object interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) IsIncreasing(object interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) IsIncreasingf(object interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) IsNonDecreasing(object interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) IsNonDecreasingf(object interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) IsNonIncreasing(object interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) IsNonIncreasingf(object interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) IsType(expectedType interface{}, object interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) IsTypef(expectedType interface{}, object interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs …interface{}) bool
    • func (a *Assertions) JSONEqf(expected string, actual string, msg string, args …interface{}) bool
    • func (a *Assertions) Len(object interface{}, length int, msgAndArgs …interface{}) bool
    • func (a *Assertions) Lenf(object interface{}, length int, msg string, args …interface{}) bool
    • func (a *Assertions) Less(e1 interface{}, e2 interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) LessOrEqual(e1 interface{}, e2 interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) LessOrEqualf(e1 interface{}, e2 interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Lessf(e1 interface{}, e2 interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Negative(e interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Negativef(e interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Never(condition func() bool, waitFor time.Duration, tick time.Duration, …) bool
    • func (a *Assertions) Neverf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, …) bool
    • func (a *Assertions) Nil(object interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Nilf(object interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) NoDirExists(path string, msgAndArgs …interface{}) bool
    • func (a *Assertions) NoDirExistsf(path string, msg string, args …interface{}) bool
    • func (a *Assertions) NoError(err error, msgAndArgs …interface{}) bool
    • func (a *Assertions) NoErrorf(err error, msg string, args …interface{}) bool
    • func (a *Assertions) NoFileExists(path string, msgAndArgs …interface{}) bool
    • func (a *Assertions) NoFileExistsf(path string, msg string, args …interface{}) bool
    • func (a *Assertions) NotContains(s interface{}, contains interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotContainsf(s interface{}, contains interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) NotEmpty(object interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotEmptyf(object interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) NotEqual(expected interface{}, actual interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotEqualValues(expected interface{}, actual interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotEqualValuesf(expected interface{}, actual interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) NotEqualf(expected interface{}, actual interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) NotErrorIs(err error, target error, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotErrorIsf(err error, target error, msg string, args …interface{}) bool
    • func (a *Assertions) NotNil(object interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotNilf(object interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) NotPanics(f PanicTestFunc, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotPanicsf(f PanicTestFunc, msg string, args …interface{}) bool
    • func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotRegexpf(rx interface{}, str interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) NotSame(expected interface{}, actual interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotSamef(expected interface{}, actual interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) NotSubset(list interface{}, subset interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotSubsetf(list interface{}, subset interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) NotZero(i interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) NotZerof(i interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Panics(f PanicTestFunc, msgAndArgs …interface{}) bool
    • func (a *Assertions) PanicsWithError(errString string, f PanicTestFunc, msgAndArgs …interface{}) bool
    • func (a *Assertions) PanicsWithErrorf(errString string, f PanicTestFunc, msg string, args …interface{}) bool
    • func (a *Assertions) PanicsWithValue(expected interface{}, f PanicTestFunc, msgAndArgs …interface{}) bool
    • func (a *Assertions) PanicsWithValuef(expected interface{}, f PanicTestFunc, msg string, args …interface{}) bool
    • func (a *Assertions) Panicsf(f PanicTestFunc, msg string, args …interface{}) bool
    • func (a *Assertions) Positive(e interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Positivef(e interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Regexpf(rx interface{}, str interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Same(expected interface{}, actual interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Samef(expected interface{}, actual interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) Subset(list interface{}, subset interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Subsetf(list interface{}, subset interface{}, msg string, args …interface{}) bool
    • func (a *Assertions) True(value bool, msgAndArgs …interface{}) bool
    • func (a *Assertions) Truef(value bool, msg string, args …interface{}) bool
    • func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta time.Duration, …) bool
    • func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta time.Duration, msg string, …) bool
    • func (a *Assertions) WithinRange(actual time.Time, start time.Time, end time.Time, msgAndArgs …interface{}) bool
    • func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Time, msg string, …) bool
    • func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs …interface{}) bool
    • func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args …interface{}) bool
    • func (a *Assertions) Zero(i interface{}, msgAndArgs …interface{}) bool
    • func (a *Assertions) Zerof(i interface{}, msg string, args …interface{}) bool
  • type BoolAssertionFunc
  • type CollectT
    • func (c *CollectT) Copy(t TestingT)
    • func (c *CollectT) Errorf(format string, args …interface{})
    • func (c *CollectT) FailNow()
    • func (c *CollectT) Reset()
  • type CompareType
  • type Comparison
  • type ComparisonAssertionFunc
  • type ErrorAssertionFunc
  • type PanicTestFunc
  • type TestingT
  • type ValueAssertionFunc
  • BoolAssertionFunc
  • ComparisonAssertionFunc
  • ErrorAssertionFunc
  • ValueAssertionFunc

This section is empty.

AnError is an error instance useful for testing. If the code does not care
about error specifics, and only needs to return the error for example, this
error should be used to make the test code more readable.

CallerInfo returns an array of strings containing the file and line number
of each stack frame leading from the current test to the assert call that
failed.

func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool

Condition uses a Comparison to assert a complex condition.

func Conditionf(t TestingT, comp Comparison, msg string, args ...interface{}) bool

Conditionf uses a Comparison to assert a complex condition.

func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool

Contains asserts that the specified string, list(array, slice…) or map contains the
specified substring or element.

assert.Contains(t, "Hello World", "World")
assert.Contains(t, ["Hello", "World"], "World")
assert.Contains(t, {"Hello": "World"}, "Hello")

func Containsf(t TestingT, s interface{}, contains interface{}, msg string, args ...interface{}) bool

Containsf asserts that the specified string, list(array, slice…) or map contains the
specified substring or element.

assert.Containsf(t, "Hello World", "World", "error message %s", "formatted")
assert.Containsf(t, ["Hello", "World"], "World", "error message %s", "formatted")
assert.Containsf(t, {"Hello": "World"}, "Hello", "error message %s", "formatted")

func DirExists(t TestingT, path string, msgAndArgs ...interface{}) bool

DirExists checks whether a directory exists in the given path. It also fails
if the path is a file rather a directory or there is an error checking whether it exists.

DirExistsf checks whether a directory exists in the given path. It also fails
if the path is a file rather a directory or there is an error checking whether it exists.

func ElementsMatch(t TestingT, listA, listB interface{}, msgAndArgs ...interface{}) (ok bool)

ElementsMatch asserts that the specified listA(array, slice…) is equal to specified
listB(array, slice…) ignoring the order of the elements. If there are duplicate elements,
the number of appearances of each of them in both lists should match.

assert.ElementsMatch(t, [1, 3, 2, 3], [1, 3, 3, 2])

func ElementsMatchf(t TestingT, listA interface{}, listB interface{}, msg string, args ...interface{}) bool

ElementsMatchf asserts that the specified listA(array, slice…) is equal to specified
listB(array, slice…) ignoring the order of the elements. If there are duplicate elements,
the number of appearances of each of them in both lists should match.

assert.ElementsMatchf(t, [1, 3, 2, 3], [1, 3, 3, 2], «error message %s», «formatted»)

func Empty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool

Empty asserts that the specified object is empty. I.e. nil, «», false, 0 or either
a slice or a channel with len == 0.

assert.Empty(t, obj)

func Emptyf(t TestingT, object interface{}, msg string, args ...interface{}) bool

Emptyf asserts that the specified object is empty. I.e. nil, «», false, 0 or either
a slice or a channel with len == 0.

assert.Emptyf(t, obj, "error message %s", "formatted")

func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

Equal asserts that two objects are equal.

assert.Equal(t, 123, 123)

Pointer variable equality is determined based on the equality of the
referenced values (as opposed to the memory addresses). Function equality
cannot be determined and will always fail.

func EqualError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) bool

EqualError asserts that a function returned an error (i.e. not `nil`)
and that it is equal to the provided error.

actualObj, err := SomeFunction()
assert.EqualError(t, err,  expectedErrorString)

EqualErrorf asserts that a function returned an error (i.e. not `nil`)
and that it is equal to the provided error.

actualObj, err := SomeFunction()
assert.EqualErrorf(t, err,  expectedErrorString, "error message %s", "formatted")

func EqualExportedValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

EqualExportedValues asserts that the types of two objects are equal and their public
fields are also equal. This is useful for comparing structs that have private fields
that could potentially differ.

 type S struct {
	Exported     	int
	notExported   	int
 }
 assert.EqualExportedValues(t, S{1, 2}, S{1, 3}) => true
 assert.EqualExportedValues(t, S{1, 2}, S{2, 3}) => false

func EqualExportedValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool

EqualExportedValuesf asserts that the types of two objects are equal and their public
fields are also equal. This is useful for comparing structs that have private fields
that could potentially differ.

 type S struct {
	Exported     	int
	notExported   	int
 }
 assert.EqualExportedValuesf(t, S{1, 2}, S{1, 3}, "error message %s", "formatted") => true
 assert.EqualExportedValuesf(t, S{1, 2}, S{2, 3}, "error message %s", "formatted") => false

func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

EqualValues asserts that two objects are equal or convertable to the same types
and equal.

assert.EqualValues(t, uint32(123), int32(123))

func EqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool

EqualValuesf asserts that two objects are equal or convertable to the same types
and equal.

assert.EqualValuesf(t, uint32(123), int32(123), "error message %s", "formatted")

func Equalf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool

Equalf asserts that two objects are equal.

assert.Equalf(t, 123, 123, "error message %s", "formatted")

Pointer variable equality is determined based on the equality of the
referenced values (as opposed to the memory addresses). Function equality
cannot be determined and will always fail.

func Error(t TestingT, err error, msgAndArgs ...interface{}) bool

Error asserts that a function returned an error (i.e. not `nil`).

  actualObj, err := SomeFunction()
  if assert.Error(t, err) {
	   assert.Equal(t, expectedError, err)
  }

func ErrorAs(t TestingT, err error, target interface{}, msgAndArgs ...interface{}) bool

ErrorAs asserts that at least one of the errors in err’s chain matches target, and if so, sets target to that error value.
This is a wrapper for errors.As.

func ErrorAsf(t TestingT, err error, target interface{}, msg string, args ...interface{}) bool

ErrorAsf asserts that at least one of the errors in err’s chain matches target, and if so, sets target to that error value.
This is a wrapper for errors.As.

func ErrorContains(t TestingT, theError error, contains string, msgAndArgs ...interface{}) bool

ErrorContains asserts that a function returned an error (i.e. not `nil`)
and that the error contains the specified substring.

actualObj, err := SomeFunction()
assert.ErrorContains(t, err,  expectedErrorSubString)

ErrorContainsf asserts that a function returned an error (i.e. not `nil`)
and that the error contains the specified substring.

actualObj, err := SomeFunction()
assert.ErrorContainsf(t, err,  expectedErrorSubString, "error message %s", "formatted")

func ErrorIs(t TestingT, err, target error, msgAndArgs ...interface{}) bool

ErrorIs asserts that at least one of the errors in err’s chain matches target.
This is a wrapper for errors.Is.

ErrorIsf asserts that at least one of the errors in err’s chain matches target.
This is a wrapper for errors.Is.

Errorf asserts that a function returned an error (i.e. not `nil`).

  actualObj, err := SomeFunction()
  if assert.Errorf(t, err, "error message %s", "formatted") {
	   assert.Equal(t, expectedErrorf, err)
  }

Eventually asserts that given condition will be met in waitFor time,
periodically checking target function each tick.

assert.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond)

EventuallyWithT asserts that given condition will be met in waitFor time,
periodically checking target function each tick. In contrast to Eventually,
it supplies a CollectT to the condition function, so that the condition
function can use the CollectT to call other assertions.
The condition is considered «met» if no errors are raised in a tick.
The supplied CollectT collects all errors from one tick (if there are any).
If the condition is not met before waitFor, the collected errors of
the last tick are copied to t.

externalValue := false
go func() {
	time.Sleep(8*time.Second)
	externalValue = true
}()
assert.EventuallyWithT(t, func(c *assert.CollectT) {
	// add assertions as needed; any assertion failure will fail the current tick
	assert.True(c, externalValue, "expected 'externalValue' to be true")
}, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still false")

EventuallyWithTf asserts that given condition will be met in waitFor time,
periodically checking target function each tick. In contrast to Eventually,
it supplies a CollectT to the condition function, so that the condition
function can use the CollectT to call other assertions.
The condition is considered «met» if no errors are raised in a tick.
The supplied CollectT collects all errors from one tick (if there are any).
If the condition is not met before waitFor, the collected errors of
the last tick are copied to t.

externalValue := false
go func() {
	time.Sleep(8*time.Second)
	externalValue = true
}()
assert.EventuallyWithTf(t, func(c *assert.CollectT, "error message %s", "formatted") {
	// add assertions as needed; any assertion failure will fail the current tick
	assert.True(c, externalValue, "expected 'externalValue' to be true")
}, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still false")

Eventuallyf asserts that given condition will be met in waitFor time,
periodically checking target function each tick.

assert.Eventuallyf(t, func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted")

func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

Exactly asserts that two objects are equal in value and type.

assert.Exactly(t, int32(123), int64(123))

func Exactlyf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool

Exactlyf asserts that two objects are equal in value and type.

assert.Exactlyf(t, int32(123), int64(123), "error message %s", "formatted")

func Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool

Fail reports a failure through

func FailNow(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool

FailNow fails test

Failf reports a failure through

func False(t TestingT, value bool, msgAndArgs ...interface{}) bool

False asserts that the specified value is false.

assert.False(t, myBool)

Falsef asserts that the specified value is false.

assert.Falsef(t, myBool, "error message %s", "formatted")

func FileExists(t TestingT, path string, msgAndArgs ...interface{}) bool

FileExists checks whether a file exists in the given path. It also fails if
the path points to a directory or there is an error when trying to check the file.

FileExistsf checks whether a file exists in the given path. It also fails if
the path points to a directory or there is an error when trying to check the file.

func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool

Greater asserts that the first element is greater than the second

assert.Greater(t, 2, 1)
assert.Greater(t, float64(2), float64(1))
assert.Greater(t, "b", "a")

func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool

GreaterOrEqual asserts that the first element is greater than or equal to the second

assert.GreaterOrEqual(t, 2, 1)
assert.GreaterOrEqual(t, 2, 2)
assert.GreaterOrEqual(t, "b", "a")
assert.GreaterOrEqual(t, "b", "b")

func GreaterOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool

GreaterOrEqualf asserts that the first element is greater than or equal to the second

assert.GreaterOrEqualf(t, 2, 1, "error message %s", "formatted")
assert.GreaterOrEqualf(t, 2, 2, "error message %s", "formatted")
assert.GreaterOrEqualf(t, "b", "a", "error message %s", "formatted")
assert.GreaterOrEqualf(t, "b", "b", "error message %s", "formatted")

func Greaterf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool

Greaterf asserts that the first element is greater than the second

assert.Greaterf(t, 2, 1, "error message %s", "formatted")
assert.Greaterf(t, float64(2), float64(1), "error message %s", "formatted")
assert.Greaterf(t, "b", "a", "error message %s", "formatted")

func HTTPBody ¶


HTTPBody is a helper that returns HTTP body of the response. It returns
empty string if building a new request fails.

func HTTPBodyContains ¶


HTTPBodyContains asserts that a specified handler returns a
body that contains a string.

assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")

Returns whether the assertion was successful (true) or not (false).

func HTTPBodyContainsf ¶

added in
v1.2.0


HTTPBodyContainsf asserts that a specified handler returns a
body that contains a string.

assert.HTTPBodyContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")

Returns whether the assertion was successful (true) or not (false).

func HTTPBodyNotContains ¶


HTTPBodyNotContains asserts that a specified handler returns a
body that does not contain a string.

assert.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")

Returns whether the assertion was successful (true) or not (false).

func HTTPBodyNotContainsf ¶

added in
v1.2.0


HTTPBodyNotContainsf asserts that a specified handler returns a
body that does not contain a string.

assert.HTTPBodyNotContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")

Returns whether the assertion was successful (true) or not (false).

HTTPError asserts that a specified handler returns an error status code.

assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}

Returns whether the assertion was successful (true) or not (false).

HTTPErrorf asserts that a specified handler returns an error status code.

assert.HTTPErrorf(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}

Returns whether the assertion was successful (true) or not (false).

HTTPRedirect asserts that a specified handler returns a redirect status code.

assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}

Returns whether the assertion was successful (true) or not (false).

HTTPRedirectf asserts that a specified handler returns a redirect status code.

assert.HTTPRedirectf(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}

Returns whether the assertion was successful (true) or not (false).

HTTPStatusCode asserts that a specified handler returns a specified status code.

assert.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501)

Returns whether the assertion was successful (true) or not (false).

HTTPStatusCodef asserts that a specified handler returns a specified status code.

assert.HTTPStatusCodef(t, myHandler, "GET", "/notImplemented", nil, 501, "error message %s", "formatted")

Returns whether the assertion was successful (true) or not (false).

HTTPSuccess asserts that a specified handler returns a success status code.

assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil)

Returns whether the assertion was successful (true) or not (false).

HTTPSuccessf asserts that a specified handler returns a success status code.

assert.HTTPSuccessf(t, myHandler, "POST", "http://www.google.com", nil, "error message %s", "formatted")

Returns whether the assertion was successful (true) or not (false).

func Implements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool

Implements asserts that an object is implemented by the specified interface.

assert.Implements(t, (*MyInterface)(nil), new(MyObject))

func Implementsf(t TestingT, interfaceObject interface{}, object interface{}, msg string, args ...interface{}) bool

Implementsf asserts that an object is implemented by the specified interface.

assert.Implementsf(t, (*MyInterface)(nil), new(MyObject), "error message %s", "formatted")

func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool

InDelta asserts that the two numerals are within delta of each other.

assert.InDelta(t, math.Pi, 22/7.0, 0.01)

func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool

InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.

func InDeltaMapValuesf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool

InDeltaMapValuesf is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.

func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool

InDeltaSlice is the same as InDelta, except it compares two slices.

func InDeltaSlicef(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool

InDeltaSlicef is the same as InDelta, except it compares two slices.

func InDeltaf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool

InDeltaf asserts that the two numerals are within delta of each other.

assert.InDeltaf(t, math.Pi, 22/7.0, 0.01, "error message %s", "formatted")

func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool

InEpsilon asserts that expected and actual have a relative error less than epsilon

func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool

InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices.

func InEpsilonSlicef(t TestingT, expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool

InEpsilonSlicef is the same as InEpsilon, except it compares each value from two slices.

func InEpsilonf(t TestingT, expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool

InEpsilonf asserts that expected and actual have a relative error less than epsilon

func IsDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool

IsDecreasing asserts that the collection is decreasing

assert.IsDecreasing(t, []int{2, 1, 0})
assert.IsDecreasing(t, []float{2, 1})
assert.IsDecreasing(t, []string{"b", "a"})

func IsDecreasingf(t TestingT, object interface{}, msg string, args ...interface{}) bool

IsDecreasingf asserts that the collection is decreasing

assert.IsDecreasingf(t, []int{2, 1, 0}, "error message %s", "formatted")
assert.IsDecreasingf(t, []float{2, 1}, "error message %s", "formatted")
assert.IsDecreasingf(t, []string{"b", "a"}, "error message %s", "formatted")

func IsIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool

IsIncreasing asserts that the collection is increasing

assert.IsIncreasing(t, []int{1, 2, 3})
assert.IsIncreasing(t, []float{1, 2})
assert.IsIncreasing(t, []string{"a", "b"})

func IsIncreasingf(t TestingT, object interface{}, msg string, args ...interface{}) bool

IsIncreasingf asserts that the collection is increasing

assert.IsIncreasingf(t, []int{1, 2, 3}, "error message %s", "formatted")
assert.IsIncreasingf(t, []float{1, 2}, "error message %s", "formatted")
assert.IsIncreasingf(t, []string{"a", "b"}, "error message %s", "formatted")

func IsNonDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool

IsNonDecreasing asserts that the collection is not decreasing

assert.IsNonDecreasing(t, []int{1, 1, 2})
assert.IsNonDecreasing(t, []float{1, 2})
assert.IsNonDecreasing(t, []string{"a", "b"})

func IsNonDecreasingf(t TestingT, object interface{}, msg string, args ...interface{}) bool

IsNonDecreasingf asserts that the collection is not decreasing

assert.IsNonDecreasingf(t, []int{1, 1, 2}, "error message %s", "formatted")
assert.IsNonDecreasingf(t, []float{1, 2}, "error message %s", "formatted")
assert.IsNonDecreasingf(t, []string{"a", "b"}, "error message %s", "formatted")

func IsNonIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool

IsNonIncreasing asserts that the collection is not increasing

assert.IsNonIncreasing(t, []int{2, 1, 1})
assert.IsNonIncreasing(t, []float{2, 1})
assert.IsNonIncreasing(t, []string{"b", "a"})

func IsNonIncreasingf(t TestingT, object interface{}, msg string, args ...interface{}) bool

IsNonIncreasingf asserts that the collection is not increasing

assert.IsNonIncreasingf(t, []int{2, 1, 1}, "error message %s", "formatted")
assert.IsNonIncreasingf(t, []float{2, 1}, "error message %s", "formatted")
assert.IsNonIncreasingf(t, []string{"b", "a"}, "error message %s", "formatted")

func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs ...interface{}) bool

IsType asserts that the specified objects are of the same type.

func IsTypef(t TestingT, expectedType interface{}, object interface{}, msg string, args ...interface{}) bool

IsTypef asserts that the specified objects are of the same type.

JSONEq asserts that two JSON strings are equivalent.

assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)

JSONEqf asserts that two JSON strings are equivalent.

assert.JSONEqf(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, "error message %s", "formatted")

func Len(t TestingT, object interface{}, length int, msgAndArgs ...interface{}) bool

Len asserts that the specified object has specific length.
Len also fails if the object has a type that len() not accept.

assert.Len(t, mySlice, 3)

func Lenf(t TestingT, object interface{}, length int, msg string, args ...interface{}) bool

Lenf asserts that the specified object has specific length.
Lenf also fails if the object has a type that len() not accept.

assert.Lenf(t, mySlice, 3, "error message %s", "formatted")

func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool

Less asserts that the first element is less than the second

assert.Less(t, 1, 2)
assert.Less(t, float64(1), float64(2))
assert.Less(t, "a", "b")

func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool

LessOrEqual asserts that the first element is less than or equal to the second

assert.LessOrEqual(t, 1, 2)
assert.LessOrEqual(t, 2, 2)
assert.LessOrEqual(t, "a", "b")
assert.LessOrEqual(t, "b", "b")

func LessOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool

LessOrEqualf asserts that the first element is less than or equal to the second

assert.LessOrEqualf(t, 1, 2, "error message %s", "formatted")
assert.LessOrEqualf(t, 2, 2, "error message %s", "formatted")
assert.LessOrEqualf(t, "a", "b", "error message %s", "formatted")
assert.LessOrEqualf(t, "b", "b", "error message %s", "formatted")

func Lessf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool

Lessf asserts that the first element is less than the second

assert.Lessf(t, 1, 2, "error message %s", "formatted")
assert.Lessf(t, float64(1), float64(2), "error message %s", "formatted")
assert.Lessf(t, "a", "b", "error message %s", "formatted")

func Negative(t TestingT, e interface{}, msgAndArgs ...interface{}) bool

Negative asserts that the specified element is negative

assert.Negative(t, -1)
assert.Negative(t, -1.23)

func Negativef(t TestingT, e interface{}, msg string, args ...interface{}) bool

Negativef asserts that the specified element is negative

assert.Negativef(t, -1, "error message %s", "formatted")
assert.Negativef(t, -1.23, "error message %s", "formatted")

Never asserts that the given condition doesn’t satisfy in waitFor time,
periodically checking the target function each tick.

assert.Never(t, func() bool { return false; }, time.Second, 10*time.Millisecond)

Neverf asserts that the given condition doesn’t satisfy in waitFor time,
periodically checking the target function each tick.

assert.Neverf(t, func() bool { return false; }, time.Second, 10*time.Millisecond, "error message %s", "formatted")

func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool

Nil asserts that the specified object is nil.

assert.Nil(t, err)

func Nilf(t TestingT, object interface{}, msg string, args ...interface{}) bool

Nilf asserts that the specified object is nil.

assert.Nilf(t, err, "error message %s", "formatted")

func NoDirExists(t TestingT, path string, msgAndArgs ...interface{}) bool

NoDirExists checks whether a directory does not exist in the given path.
It fails if the path points to an existing _directory_ only.

NoDirExistsf checks whether a directory does not exist in the given path.
It fails if the path points to an existing _directory_ only.

func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool

NoError asserts that a function returned no error (i.e. `nil`).

  actualObj, err := SomeFunction()
  if assert.NoError(t, err) {
	   assert.Equal(t, expectedObj, actualObj)
  }

NoErrorf asserts that a function returned no error (i.e. `nil`).

  actualObj, err := SomeFunction()
  if assert.NoErrorf(t, err, "error message %s", "formatted") {
	   assert.Equal(t, expectedObj, actualObj)
  }

func NoFileExists(t TestingT, path string, msgAndArgs ...interface{}) bool

NoFileExists checks whether a file does not exist in a given path. It fails
if the path points to an existing _file_ only.

NoFileExistsf checks whether a file does not exist in a given path. It fails
if the path points to an existing _file_ only.

func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool

NotContains asserts that the specified string, list(array, slice…) or map does NOT contain the
specified substring or element.

assert.NotContains(t, "Hello World", "Earth")
assert.NotContains(t, ["Hello", "World"], "Earth")
assert.NotContains(t, {"Hello": "World"}, "Earth")

func NotContainsf(t TestingT, s interface{}, contains interface{}, msg string, args ...interface{}) bool

NotContainsf asserts that the specified string, list(array, slice…) or map does NOT contain the
specified substring or element.

assert.NotContainsf(t, "Hello World", "Earth", "error message %s", "formatted")
assert.NotContainsf(t, ["Hello", "World"], "Earth", "error message %s", "formatted")
assert.NotContainsf(t, {"Hello": "World"}, "Earth", "error message %s", "formatted")

func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool

NotEmpty asserts that the specified object is NOT empty. I.e. not nil, «», false, 0 or either
a slice or a channel with len == 0.

if assert.NotEmpty(t, obj) {
  assert.Equal(t, "two", obj[1])
}

func NotEmptyf(t TestingT, object interface{}, msg string, args ...interface{}) bool

NotEmptyf asserts that the specified object is NOT empty. I.e. not nil, «», false, 0 or either
a slice or a channel with len == 0.

if assert.NotEmptyf(t, obj, "error message %s", "formatted") {
  assert.Equal(t, "two", obj[1])
}

func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

NotEqual asserts that the specified values are NOT equal.

assert.NotEqual(t, obj1, obj2)

Pointer variable equality is determined based on the equality of the
referenced values (as opposed to the memory addresses).

func NotEqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

NotEqualValues asserts that two objects are not equal even when converted to the same type

assert.NotEqualValues(t, obj1, obj2)

func NotEqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool

NotEqualValuesf asserts that two objects are not equal even when converted to the same type

assert.NotEqualValuesf(t, obj1, obj2, "error message %s", "formatted")

func NotEqualf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool

NotEqualf asserts that the specified values are NOT equal.

assert.NotEqualf(t, obj1, obj2, "error message %s", "formatted")

Pointer variable equality is determined based on the equality of the
referenced values (as opposed to the memory addresses).

func NotErrorIs(t TestingT, err, target error, msgAndArgs ...interface{}) bool

NotErrorIs asserts that at none of the errors in err’s chain matches target.
This is a wrapper for errors.Is.

NotErrorIsf asserts that at none of the errors in err’s chain matches target.
This is a wrapper for errors.Is.

func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool

NotNil asserts that the specified object is not nil.

assert.NotNil(t, err)

func NotNilf(t TestingT, object interface{}, msg string, args ...interface{}) bool

NotNilf asserts that the specified object is not nil.

assert.NotNilf(t, err, "error message %s", "formatted")

func NotPanics(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool

NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic.

assert.NotPanics(t, func(){ RemainCalm() })

func NotPanicsf(t TestingT, f PanicTestFunc, msg string, args ...interface{}) bool

NotPanicsf asserts that the code inside the specified PanicTestFunc does NOT panic.

assert.NotPanicsf(t, func(){ RemainCalm() }, "error message %s", "formatted")

func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) bool

NotRegexp asserts that a specified regexp does not match a string.

assert.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
assert.NotRegexp(t, "^start", "it's not starting")

func NotRegexpf(t TestingT, rx interface{}, str interface{}, msg string, args ...interface{}) bool

NotRegexpf asserts that a specified regexp does not match a string.

assert.NotRegexpf(t, regexp.MustCompile("starts"), "it's starting", "error message %s", "formatted")
assert.NotRegexpf(t, "^start", "it's not starting", "error message %s", "formatted")

func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

NotSame asserts that two pointers do not reference the same object.

assert.NotSame(t, ptr1, ptr2)

Both arguments must be pointer variables. Pointer variable sameness is
determined based on the equality of both type and value.

func NotSamef(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool

NotSamef asserts that two pointers do not reference the same object.

assert.NotSamef(t, ptr1, ptr2, "error message %s", "formatted")

Both arguments must be pointer variables. Pointer variable sameness is
determined based on the equality of both type and value.

func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool)

NotSubset asserts that the specified list(array, slice…) contains not all
elements given in the specified subset(array, slice…).

assert.NotSubset(t, [1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]")

func NotSubsetf(t TestingT, list interface{}, subset interface{}, msg string, args ...interface{}) bool

NotSubsetf asserts that the specified list(array, slice…) contains not all
elements given in the specified subset(array, slice…).

assert.NotSubsetf(t, [1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]", "error message %s", "formatted")

func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool

NotZero asserts that i is not the zero value for its type.

func NotZerof(t TestingT, i interface{}, msg string, args ...interface{}) bool

NotZerof asserts that i is not the zero value for its type.

func ObjectsAreEqual(expected, actual interface{}) bool

ObjectsAreEqual determines if two objects are considered equal.

This function does no assertion of any kind.

func ObjectsAreEqualValues(expected, actual interface{}) bool

ObjectsAreEqualValues gets whether two objects are equal, or if their
values are equal.

func ObjectsExportedFieldsAreEqual(expected, actual interface{}) bool

ObjectsExportedFieldsAreEqual determines if the exported (public) fields of two objects are
considered equal. This comparison of only exported fields is applied recursively to nested data
structures.

This function does no assertion of any kind.

func Panics(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool

Panics asserts that the code inside the specified PanicTestFunc panics.

assert.Panics(t, func(){ GoCrazy() })

func PanicsWithError(t TestingT, errString string, f PanicTestFunc, msgAndArgs ...interface{}) bool

PanicsWithError asserts that the code inside the specified PanicTestFunc
panics, and that the recovered panic value is an error that satisfies the
EqualError comparison.

assert.PanicsWithError(t, "crazy error", func(){ GoCrazy() })

PanicsWithErrorf asserts that the code inside the specified PanicTestFunc
panics, and that the recovered panic value is an error that satisfies the
EqualError comparison.

assert.PanicsWithErrorf(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted")

func PanicsWithValue(t TestingT, expected interface{}, f PanicTestFunc, msgAndArgs ...interface{}) bool

PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that
the recovered panic value equals the expected panic value.

assert.PanicsWithValue(t, "crazy error", func(){ GoCrazy() })

func PanicsWithValuef(t TestingT, expected interface{}, f PanicTestFunc, msg string, args ...interface{}) bool

PanicsWithValuef asserts that the code inside the specified PanicTestFunc panics, and that
the recovered panic value equals the expected panic value.

assert.PanicsWithValuef(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted")

Panicsf asserts that the code inside the specified PanicTestFunc panics.

assert.Panicsf(t, func(){ GoCrazy() }, "error message %s", "formatted")

func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool

Positive asserts that the specified element is positive

assert.Positive(t, 1)
assert.Positive(t, 1.23)

func Positivef(t TestingT, e interface{}, msg string, args ...interface{}) bool

Positivef asserts that the specified element is positive

assert.Positivef(t, 1, "error message %s", "formatted")
assert.Positivef(t, 1.23, "error message %s", "formatted")

func Regexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) bool

Regexp asserts that a specified regexp matches a string.

assert.Regexp(t, regexp.MustCompile("start"), "it's starting")
assert.Regexp(t, "start...$", "it's not starting")

func Regexpf(t TestingT, rx interface{}, str interface{}, msg string, args ...interface{}) bool

Regexpf asserts that a specified regexp matches a string.

assert.Regexpf(t, regexp.MustCompile("start"), "it's starting", "error message %s", "formatted")
assert.Regexpf(t, "start...$", "it's not starting", "error message %s", "formatted")

func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool

Same asserts that two pointers reference the same object.

assert.Same(t, ptr1, ptr2)

Both arguments must be pointer variables. Pointer variable sameness is
determined based on the equality of both type and value.

func Samef(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool

Samef asserts that two pointers reference the same object.

assert.Samef(t, ptr1, ptr2, "error message %s", "formatted")

Both arguments must be pointer variables. Pointer variable sameness is
determined based on the equality of both type and value.

func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool)

Subset asserts that the specified list(array, slice…) contains all
elements given in the specified subset(array, slice…).

assert.Subset(t, [1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]")

func Subsetf(t TestingT, list interface{}, subset interface{}, msg string, args ...interface{}) bool

Subsetf asserts that the specified list(array, slice…) contains all
elements given in the specified subset(array, slice…).

assert.Subsetf(t, [1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]", "error message %s", "formatted")

func True(t TestingT, value bool, msgAndArgs ...interface{}) bool

True asserts that the specified value is true.

assert.True(t, myBool)

Truef asserts that the specified value is true.

assert.Truef(t, myBool, "error message %s", "formatted")

WithinDuration asserts that the two times are within duration delta of each other.

assert.WithinDuration(t, time.Now(), time.Now(), 10*time.Second)

WithinDurationf asserts that the two times are within duration delta of each other.

assert.WithinDurationf(t, time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted")

func WithinRange(t TestingT, actual, start, end time.Time, msgAndArgs ...interface{}) bool

WithinRange asserts that a time is within a time range (inclusive).

assert.WithinRange(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second))

WithinRangef asserts that a time is within a time range (inclusive).

assert.WithinRangef(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second), "error message %s", "formatted")

YAMLEq asserts that two YAML strings are equivalent.

YAMLEqf asserts that two YAML strings are equivalent.

func Zero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool

Zero asserts that i is the zero value for its type.

func Zerof(t TestingT, i interface{}, msg string, args ...interface{}) bool

Zerof asserts that i is the zero value for its type.

type Assertions struct {
	
}

Assertions provides assertion methods around the
TestingT interface.

func New(t TestingT) *Assertions

New makes a new Assertions object for the specified TestingT.

func (a *Assertions) Condition(comp Comparison, msgAndArgs ...interface{}) bool

Condition uses a Comparison to assert a complex condition.

func (a *Assertions) Conditionf(comp Comparison, msg string, args ...interface{}) bool

Conditionf uses a Comparison to assert a complex condition.

func (a *Assertions) Contains(s interface{}, contains interface{}, msgAndArgs ...interface{}) bool

Contains asserts that the specified string, list(array, slice…) or map contains the
specified substring or element.

a.Contains("Hello World", "World")
a.Contains(["Hello", "World"], "World")
a.Contains({"Hello": "World"}, "Hello")

func (a *Assertions) Containsf(s interface{}, contains interface{}, msg string, args ...interface{}) bool

Containsf asserts that the specified string, list(array, slice…) or map contains the
specified substring or element.

a.Containsf("Hello World", "World", "error message %s", "formatted")
a.Containsf(["Hello", "World"], "World", "error message %s", "formatted")
a.Containsf({"Hello": "World"}, "Hello", "error message %s", "formatted")

func (a *Assertions) DirExists(path string, msgAndArgs ...interface{}) bool

DirExists checks whether a directory exists in the given path. It also fails
if the path is a file rather a directory or there is an error checking whether it exists.

DirExistsf checks whether a directory exists in the given path. It also fails
if the path is a file rather a directory or there is an error checking whether it exists.

func (a *Assertions) ElementsMatch(listA interface{}, listB interface{}, msgAndArgs ...interface{}) bool

ElementsMatch asserts that the specified listA(array, slice…) is equal to specified
listB(array, slice…) ignoring the order of the elements. If there are duplicate elements,
the number of appearances of each of them in both lists should match.

a.ElementsMatch([1, 3, 2, 3], [1, 3, 3, 2])

func (a *Assertions) ElementsMatchf(listA interface{}, listB interface{}, msg string, args ...interface{}) bool

ElementsMatchf asserts that the specified listA(array, slice…) is equal to specified
listB(array, slice…) ignoring the order of the elements. If there are duplicate elements,
the number of appearances of each of them in both lists should match.

a.ElementsMatchf([1, 3, 2, 3], [1, 3, 3, 2], «error message %s», «formatted»)

func (a *Assertions) Empty(object interface{}, msgAndArgs ...interface{}) bool

Empty asserts that the specified object is empty. I.e. nil, «», false, 0 or either
a slice or a channel with len == 0.

a.Empty(obj)

func (a *Assertions) Emptyf(object interface{}, msg string, args ...interface{}) bool

Emptyf asserts that the specified object is empty. I.e. nil, «», false, 0 or either
a slice or a channel with len == 0.

a.Emptyf(obj, "error message %s", "formatted")

func (a *Assertions) Equal(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool

Equal asserts that two objects are equal.

a.Equal(123, 123)

Pointer variable equality is determined based on the equality of the
referenced values (as opposed to the memory addresses). Function equality
cannot be determined and will always fail.

func (a *Assertions) EqualError(theError error, errString string, msgAndArgs ...interface{}) bool

EqualError asserts that a function returned an error (i.e. not `nil`)
and that it is equal to the provided error.

actualObj, err := SomeFunction()
a.EqualError(err,  expectedErrorString)

EqualErrorf asserts that a function returned an error (i.e. not `nil`)
and that it is equal to the provided error.

actualObj, err := SomeFunction()
a.EqualErrorf(err,  expectedErrorString, "error message %s", "formatted")

func (a *Assertions) EqualExportedValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool

EqualExportedValues asserts that the types of two objects are equal and their public
fields are also equal. This is useful for comparing structs that have private fields
that could potentially differ.

 type S struct {
	Exported     	int
	notExported   	int
 }
 a.EqualExportedValues(S{1, 2}, S{1, 3}) => true
 a.EqualExportedValues(S{1, 2}, S{2, 3}) => false

func (a *Assertions) EqualExportedValuesf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool

EqualExportedValuesf asserts that the types of two objects are equal and their public
fields are also equal. This is useful for comparing structs that have private fields
that could potentially differ.

 type S struct {
	Exported     	int
	notExported   	int
 }
 a.EqualExportedValuesf(S{1, 2}, S{1, 3}, "error message %s", "formatted") => true
 a.EqualExportedValuesf(S{1, 2}, S{2, 3}, "error message %s", "formatted") => false

func (a *Assertions) EqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool

EqualValues asserts that two objects are equal or convertable to the same types
and equal.

a.EqualValues(uint32(123), int32(123))

func (a *Assertions) EqualValuesf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool

EqualValuesf asserts that two objects are equal or convertable to the same types
and equal.

a.EqualValuesf(uint32(123), int32(123), "error message %s", "formatted")

func (a *Assertions) Equalf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool

Equalf asserts that two objects are equal.

a.Equalf(123, 123, "error message %s", "formatted")

Pointer variable equality is determined based on the equality of the
referenced values (as opposed to the memory addresses). Function equality
cannot be determined and will always fail.

func (a *Assertions) Error(err error, msgAndArgs ...interface{}) bool

Error asserts that a function returned an error (i.e. not `nil`).

  actualObj, err := SomeFunction()
  if a.Error(err) {
	   assert.Equal(t, expectedError, err)
  }

func (a *Assertions) ErrorAs(err error, target interface{}, msgAndArgs ...interface{}) bool

ErrorAs asserts that at least one of the errors in err’s chain matches target, and if so, sets target to that error value.
This is a wrapper for errors.As.

func (a *Assertions) ErrorAsf(err error, target interface{}, msg string, args ...interface{}) bool

ErrorAsf asserts that at least one of the errors in err’s chain matches target, and if so, sets target to that error value.
This is a wrapper for errors.As.

func (a *Assertions) ErrorContains(theError error, contains string, msgAndArgs ...interface{}) bool

ErrorContains asserts that a function returned an error (i.e. not `nil`)
and that the error contains the specified substring.

actualObj, err := SomeFunction()
a.ErrorContains(err,  expectedErrorSubString)

ErrorContainsf asserts that a function returned an error (i.e. not `nil`)
and that the error contains the specified substring.

actualObj, err := SomeFunction()
a.ErrorContainsf(err,  expectedErrorSubString, "error message %s", "formatted")

func (a *Assertions) ErrorIs(err error, target error, msgAndArgs ...interface{}) bool

ErrorIs asserts that at least one of the errors in err’s chain matches target.
This is a wrapper for errors.Is.

ErrorIsf asserts that at least one of the errors in err’s chain matches target.
This is a wrapper for errors.Is.

Errorf asserts that a function returned an error (i.e. not `nil`).

  actualObj, err := SomeFunction()
  if a.Errorf(err, "error message %s", "formatted") {
	   assert.Equal(t, expectedErrorf, err)
  }

Eventually asserts that given condition will be met in waitFor time,
periodically checking target function each tick.

a.Eventually(func() bool { return true; }, time.Second, 10*time.Millisecond)

EventuallyWithT asserts that given condition will be met in waitFor time,
periodically checking target function each tick. In contrast to Eventually,
it supplies a CollectT to the condition function, so that the condition
function can use the CollectT to call other assertions.
The condition is considered «met» if no errors are raised in a tick.
The supplied CollectT collects all errors from one tick (if there are any).
If the condition is not met before waitFor, the collected errors of
the last tick are copied to t.

externalValue := false
go func() {
	time.Sleep(8*time.Second)
	externalValue = true
}()
a.EventuallyWithT(func(c *assert.CollectT) {
	// add assertions as needed; any assertion failure will fail the current tick
	assert.True(c, externalValue, "expected 'externalValue' to be true")
}, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still false")

EventuallyWithTf asserts that given condition will be met in waitFor time,
periodically checking target function each tick. In contrast to Eventually,
it supplies a CollectT to the condition function, so that the condition
function can use the CollectT to call other assertions.
The condition is considered «met» if no errors are raised in a tick.
The supplied CollectT collects all errors from one tick (if there are any).
If the condition is not met before waitFor, the collected errors of
the last tick are copied to t.

externalValue := false
go func() {
	time.Sleep(8*time.Second)
	externalValue = true
}()
a.EventuallyWithTf(func(c *assert.CollectT, "error message %s", "formatted") {
	// add assertions as needed; any assertion failure will fail the current tick
	assert.True(c, externalValue, "expected 'externalValue' to be true")
}, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still false")

Eventuallyf asserts that given condition will be met in waitFor time,
periodically checking target function each tick.

a.Eventuallyf(func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted")

func (a *Assertions) Exactly(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool

Exactly asserts that two objects are equal in value and type.

a.Exactly(int32(123), int64(123))

func (a *Assertions) Exactlyf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool

Exactlyf asserts that two objects are equal in value and type.

a.Exactlyf(int32(123), int64(123), "error message %s", "formatted")

func (a *Assertions) Fail(failureMessage string, msgAndArgs ...interface{}) bool

Fail reports a failure through

func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...interface{}) bool

FailNow fails test

Failf reports a failure through

func (a *Assertions) False(value bool, msgAndArgs ...interface{}) bool

False asserts that the specified value is false.

a.False(myBool)

Falsef asserts that the specified value is false.

a.Falsef(myBool, "error message %s", "formatted")

func (a *Assertions) FileExists(path string, msgAndArgs ...interface{}) bool

FileExists checks whether a file exists in the given path. It also fails if
the path points to a directory or there is an error when trying to check the file.

FileExistsf checks whether a file exists in the given path. It also fails if
the path points to a directory or there is an error when trying to check the file.

func (a *Assertions) Greater(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool

Greater asserts that the first element is greater than the second

a.Greater(2, 1)
a.Greater(float64(2), float64(1))
a.Greater("b", "a")

func (a *Assertions) GreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool

GreaterOrEqual asserts that the first element is greater than or equal to the second

a.GreaterOrEqual(2, 1)
a.GreaterOrEqual(2, 2)
a.GreaterOrEqual("b", "a")
a.GreaterOrEqual("b", "b")

func (a *Assertions) GreaterOrEqualf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool

GreaterOrEqualf asserts that the first element is greater than or equal to the second

a.GreaterOrEqualf(2, 1, "error message %s", "formatted")
a.GreaterOrEqualf(2, 2, "error message %s", "formatted")
a.GreaterOrEqualf("b", "a", "error message %s", "formatted")
a.GreaterOrEqualf("b", "b", "error message %s", "formatted")

func (a *Assertions) Greaterf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool

Greaterf asserts that the first element is greater than the second

a.Greaterf(2, 1, "error message %s", "formatted")
a.Greaterf(float64(2), float64(1), "error message %s", "formatted")
a.Greaterf("b", "a", "error message %s", "formatted")

func (*Assertions) HTTPBodyContains ¶


HTTPBodyContains asserts that a specified handler returns a
body that contains a string.

a.HTTPBodyContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")

Returns whether the assertion was successful (true) or not (false).

func (*Assertions) HTTPBodyContainsf ¶

added in
v1.2.0


HTTPBodyContainsf asserts that a specified handler returns a
body that contains a string.

a.HTTPBodyContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")

Returns whether the assertion was successful (true) or not (false).

func (*Assertions) HTTPBodyNotContains ¶


HTTPBodyNotContains asserts that a specified handler returns a
body that does not contain a string.

a.HTTPBodyNotContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")

Returns whether the assertion was successful (true) or not (false).

func (*Assertions) HTTPBodyNotContainsf ¶

added in
v1.2.0


HTTPBodyNotContainsf asserts that a specified handler returns a
body that does not contain a string.

a.HTTPBodyNotContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")

Returns whether the assertion was successful (true) or not (false).

HTTPError asserts that a specified handler returns an error status code.

a.HTTPError(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}

Returns whether the assertion was successful (true) or not (false).

HTTPErrorf asserts that a specified handler returns an error status code.

a.HTTPErrorf(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}

Returns whether the assertion was successful (true) or not (false).

HTTPRedirect asserts that a specified handler returns a redirect status code.

a.HTTPRedirect(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}

Returns whether the assertion was successful (true) or not (false).

HTTPRedirectf asserts that a specified handler returns a redirect status code.

a.HTTPRedirectf(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}

Returns whether the assertion was successful (true) or not (false).

HTTPStatusCode asserts that a specified handler returns a specified status code.

a.HTTPStatusCode(myHandler, "GET", "/notImplemented", nil, 501)

Returns whether the assertion was successful (true) or not (false).

HTTPStatusCodef asserts that a specified handler returns a specified status code.

a.HTTPStatusCodef(myHandler, "GET", "/notImplemented", nil, 501, "error message %s", "formatted")

Returns whether the assertion was successful (true) or not (false).

HTTPSuccess asserts that a specified handler returns a success status code.

a.HTTPSuccess(myHandler, "POST", "http://www.google.com", nil)

Returns whether the assertion was successful (true) or not (false).

HTTPSuccessf asserts that a specified handler returns a success status code.

a.HTTPSuccessf(myHandler, "POST", "http://www.google.com", nil, "error message %s", "formatted")

Returns whether the assertion was successful (true) or not (false).

func (a *Assertions) Implements(interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool

Implements asserts that an object is implemented by the specified interface.

a.Implements((*MyInterface)(nil), new(MyObject))

func (a *Assertions) Implementsf(interfaceObject interface{}, object interface{}, msg string, args ...interface{}) bool

Implementsf asserts that an object is implemented by the specified interface.

a.Implementsf((*MyInterface)(nil), new(MyObject), "error message %s", "formatted")

func (a *Assertions) InDelta(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool

InDelta asserts that the two numerals are within delta of each other.

a.InDelta(math.Pi, 22/7.0, 0.01)

func (a *Assertions) InDeltaMapValues(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool

InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.

func (a *Assertions) InDeltaMapValuesf(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool

InDeltaMapValuesf is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.

func (a *Assertions) InDeltaSlice(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool

InDeltaSlice is the same as InDelta, except it compares two slices.

func (a *Assertions) InDeltaSlicef(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool

InDeltaSlicef is the same as InDelta, except it compares two slices.

func (a *Assertions) InDeltaf(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool

InDeltaf asserts that the two numerals are within delta of each other.

a.InDeltaf(math.Pi, 22/7.0, 0.01, "error message %s", "formatted")

func (a *Assertions) InEpsilon(expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool

InEpsilon asserts that expected and actual have a relative error less than epsilon

func (a *Assertions) InEpsilonSlice(expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool

InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices.

func (a *Assertions) InEpsilonSlicef(expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool

InEpsilonSlicef is the same as InEpsilon, except it compares each value from two slices.

func (a *Assertions) InEpsilonf(expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool

InEpsilonf asserts that expected and actual have a relative error less than epsilon

func (a *Assertions) IsDecreasing(object interface{}, msgAndArgs ...interface{}) bool

IsDecreasing asserts that the collection is decreasing

a.IsDecreasing([]int{2, 1, 0})
a.IsDecreasing([]float{2, 1})
a.IsDecreasing([]string{"b", "a"})

func (a *Assertions) IsDecreasingf(object interface{}, msg string, args ...interface{}) bool

IsDecreasingf asserts that the collection is decreasing

a.IsDecreasingf([]int{2, 1, 0}, "error message %s", "formatted")
a.IsDecreasingf([]float{2, 1}, "error message %s", "formatted")
a.IsDecreasingf([]string{"b", "a"}, "error message %s", "formatted")

func (a *Assertions) IsIncreasing(object interface{}, msgAndArgs ...interface{}) bool

IsIncreasing asserts that the collection is increasing

a.IsIncreasing([]int{1, 2, 3})
a.IsIncreasing([]float{1, 2})
a.IsIncreasing([]string{"a", "b"})

func (a *Assertions) IsIncreasingf(object interface{}, msg string, args ...interface{}) bool

IsIncreasingf asserts that the collection is increasing

a.IsIncreasingf([]int{1, 2, 3}, "error message %s", "formatted")
a.IsIncreasingf([]float{1, 2}, "error message %s", "formatted")
a.IsIncreasingf([]string{"a", "b"}, "error message %s", "formatted")

func (a *Assertions) IsNonDecreasing(object interface{}, msgAndArgs ...interface{}) bool

IsNonDecreasing asserts that the collection is not decreasing

a.IsNonDecreasing([]int{1, 1, 2})
a.IsNonDecreasing([]float{1, 2})
a.IsNonDecreasing([]string{"a", "b"})

func (a *Assertions) IsNonDecreasingf(object interface{}, msg string, args ...interface{}) bool

IsNonDecreasingf asserts that the collection is not decreasing

a.IsNonDecreasingf([]int{1, 1, 2}, "error message %s", "formatted")
a.IsNonDecreasingf([]float{1, 2}, "error message %s", "formatted")
a.IsNonDecreasingf([]string{"a", "b"}, "error message %s", "formatted")

func (a *Assertions) IsNonIncreasing(object interface{}, msgAndArgs ...interface{}) bool

IsNonIncreasing asserts that the collection is not increasing

a.IsNonIncreasing([]int{2, 1, 1})
a.IsNonIncreasing([]float{2, 1})
a.IsNonIncreasing([]string{"b", "a"})

func (a *Assertions) IsNonIncreasingf(object interface{}, msg string, args ...interface{}) bool

IsNonIncreasingf asserts that the collection is not increasing

a.IsNonIncreasingf([]int{2, 1, 1}, "error message %s", "formatted")
a.IsNonIncreasingf([]float{2, 1}, "error message %s", "formatted")
a.IsNonIncreasingf([]string{"b", "a"}, "error message %s", "formatted")

func (a *Assertions) IsType(expectedType interface{}, object interface{}, msgAndArgs ...interface{}) bool

IsType asserts that the specified objects are of the same type.

func (a *Assertions) IsTypef(expectedType interface{}, object interface{}, msg string, args ...interface{}) bool

IsTypef asserts that the specified objects are of the same type.

JSONEq asserts that two JSON strings are equivalent.

a.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)

JSONEqf asserts that two JSON strings are equivalent.

a.JSONEqf(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, "error message %s", "formatted")

func (a *Assertions) Len(object interface{}, length int, msgAndArgs ...interface{}) bool

Len asserts that the specified object has specific length.
Len also fails if the object has a type that len() not accept.

a.Len(mySlice, 3)

func (a *Assertions) Lenf(object interface{}, length int, msg string, args ...interface{}) bool

Lenf asserts that the specified object has specific length.
Lenf also fails if the object has a type that len() not accept.

a.Lenf(mySlice, 3, "error message %s", "formatted")

func (a *Assertions) Less(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool

Less asserts that the first element is less than the second

a.Less(1, 2)
a.Less(float64(1), float64(2))
a.Less("a", "b")

func (a *Assertions) LessOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool

LessOrEqual asserts that the first element is less than or equal to the second

a.LessOrEqual(1, 2)
a.LessOrEqual(2, 2)
a.LessOrEqual("a", "b")
a.LessOrEqual("b", "b")

func (a *Assertions) LessOrEqualf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool

LessOrEqualf asserts that the first element is less than or equal to the second

a.LessOrEqualf(1, 2, "error message %s", "formatted")
a.LessOrEqualf(2, 2, "error message %s", "formatted")
a.LessOrEqualf("a", "b", "error message %s", "formatted")
a.LessOrEqualf("b", "b", "error message %s", "formatted")

func (a *Assertions) Lessf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool

Lessf asserts that the first element is less than the second

a.Lessf(1, 2, "error message %s", "formatted")
a.Lessf(float64(1), float64(2), "error message %s", "formatted")
a.Lessf("a", "b", "error message %s", "formatted")

func (a *Assertions) Negative(e interface{}, msgAndArgs ...interface{}) bool

Negative asserts that the specified element is negative

a.Negative(-1)
a.Negative(-1.23)

func (a *Assertions) Negativef(e interface{}, msg string, args ...interface{}) bool

Negativef asserts that the specified element is negative

a.Negativef(-1, "error message %s", "formatted")
a.Negativef(-1.23, "error message %s", "formatted")

Never asserts that the given condition doesn’t satisfy in waitFor time,
periodically checking the target function each tick.

a.Never(func() bool { return false; }, time.Second, 10*time.Millisecond)

Neverf asserts that the given condition doesn’t satisfy in waitFor time,
periodically checking the target function each tick.

a.Neverf(func() bool { return false; }, time.Second, 10*time.Millisecond, "error message %s", "formatted")

func (a *Assertions) Nil(object interface{}, msgAndArgs ...interface{}) bool

Nil asserts that the specified object is nil.

a.Nil(err)

func (a *Assertions) Nilf(object interface{}, msg string, args ...interface{}) bool

Nilf asserts that the specified object is nil.

a.Nilf(err, "error message %s", "formatted")

func (a *Assertions) NoDirExists(path string, msgAndArgs ...interface{}) bool

NoDirExists checks whether a directory does not exist in the given path.
It fails if the path points to an existing _directory_ only.

NoDirExistsf checks whether a directory does not exist in the given path.
It fails if the path points to an existing _directory_ only.

func (a *Assertions) NoError(err error, msgAndArgs ...interface{}) bool

NoError asserts that a function returned no error (i.e. `nil`).

  actualObj, err := SomeFunction()
  if a.NoError(err) {
	   assert.Equal(t, expectedObj, actualObj)
  }

NoErrorf asserts that a function returned no error (i.e. `nil`).

  actualObj, err := SomeFunction()
  if a.NoErrorf(err, "error message %s", "formatted") {
	   assert.Equal(t, expectedObj, actualObj)
  }

func (a *Assertions) NoFileExists(path string, msgAndArgs ...interface{}) bool

NoFileExists checks whether a file does not exist in a given path. It fails
if the path points to an existing _file_ only.

NoFileExistsf checks whether a file does not exist in a given path. It fails
if the path points to an existing _file_ only.

func (a *Assertions) NotContains(s interface{}, contains interface{}, msgAndArgs ...interface{}) bool

NotContains asserts that the specified string, list(array, slice…) or map does NOT contain the
specified substring or element.

a.NotContains("Hello World", "Earth")
a.NotContains(["Hello", "World"], "Earth")
a.NotContains({"Hello": "World"}, "Earth")

func (a *Assertions) NotContainsf(s interface{}, contains interface{}, msg string, args ...interface{}) bool

NotContainsf asserts that the specified string, list(array, slice…) or map does NOT contain the
specified substring or element.

a.NotContainsf("Hello World", "Earth", "error message %s", "formatted")
a.NotContainsf(["Hello", "World"], "Earth", "error message %s", "formatted")
a.NotContainsf({"Hello": "World"}, "Earth", "error message %s", "formatted")

func (a *Assertions) NotEmpty(object interface{}, msgAndArgs ...interface{}) bool

NotEmpty asserts that the specified object is NOT empty. I.e. not nil, «», false, 0 or either
a slice or a channel with len == 0.

if a.NotEmpty(obj) {
  assert.Equal(t, "two", obj[1])
}

func (a *Assertions) NotEmptyf(object interface{}, msg string, args ...interface{}) bool

NotEmptyf asserts that the specified object is NOT empty. I.e. not nil, «», false, 0 or either
a slice or a channel with len == 0.

if a.NotEmptyf(obj, "error message %s", "formatted") {
  assert.Equal(t, "two", obj[1])
}

func (a *Assertions) NotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool

NotEqual asserts that the specified values are NOT equal.

a.NotEqual(obj1, obj2)

Pointer variable equality is determined based on the equality of the
referenced values (as opposed to the memory addresses).

func (a *Assertions) NotEqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool

NotEqualValues asserts that two objects are not equal even when converted to the same type

a.NotEqualValues(obj1, obj2)

func (a *Assertions) NotEqualValuesf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool

NotEqualValuesf asserts that two objects are not equal even when converted to the same type

a.NotEqualValuesf(obj1, obj2, "error message %s", "formatted")

func (a *Assertions) NotEqualf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool

NotEqualf asserts that the specified values are NOT equal.

a.NotEqualf(obj1, obj2, "error message %s", "formatted")

Pointer variable equality is determined based on the equality of the
referenced values (as opposed to the memory addresses).

func (a *Assertions) NotErrorIs(err error, target error, msgAndArgs ...interface{}) bool

NotErrorIs asserts that at none of the errors in err’s chain matches target.
This is a wrapper for errors.Is.

NotErrorIsf asserts that at none of the errors in err’s chain matches target.
This is a wrapper for errors.Is.

func (a *Assertions) NotNil(object interface{}, msgAndArgs ...interface{}) bool

NotNil asserts that the specified object is not nil.

a.NotNil(err)

func (a *Assertions) NotNilf(object interface{}, msg string, args ...interface{}) bool

NotNilf asserts that the specified object is not nil.

a.NotNilf(err, "error message %s", "formatted")

func (a *Assertions) NotPanics(f PanicTestFunc, msgAndArgs ...interface{}) bool

NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic.

a.NotPanics(func(){ RemainCalm() })

func (a *Assertions) NotPanicsf(f PanicTestFunc, msg string, args ...interface{}) bool

NotPanicsf asserts that the code inside the specified PanicTestFunc does NOT panic.

a.NotPanicsf(func(){ RemainCalm() }, "error message %s", "formatted")

func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool

NotRegexp asserts that a specified regexp does not match a string.

a.NotRegexp(regexp.MustCompile("starts"), "it's starting")
a.NotRegexp("^start", "it's not starting")

func (a *Assertions) NotRegexpf(rx interface{}, str interface{}, msg string, args ...interface{}) bool

NotRegexpf asserts that a specified regexp does not match a string.

a.NotRegexpf(regexp.MustCompile("starts"), "it's starting", "error message %s", "formatted")
a.NotRegexpf("^start", "it's not starting", "error message %s", "formatted")

func (a *Assertions) NotSame(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool

NotSame asserts that two pointers do not reference the same object.

a.NotSame(ptr1, ptr2)

Both arguments must be pointer variables. Pointer variable sameness is
determined based on the equality of both type and value.

func (a *Assertions) NotSamef(expected interface{}, actual interface{}, msg string, args ...interface{}) bool

NotSamef asserts that two pointers do not reference the same object.

a.NotSamef(ptr1, ptr2, "error message %s", "formatted")

Both arguments must be pointer variables. Pointer variable sameness is
determined based on the equality of both type and value.

func (a *Assertions) NotSubset(list interface{}, subset interface{}, msgAndArgs ...interface{}) bool

NotSubset asserts that the specified list(array, slice…) contains not all
elements given in the specified subset(array, slice…).

a.NotSubset([1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]")

func (a *Assertions) NotSubsetf(list interface{}, subset interface{}, msg string, args ...interface{}) bool

NotSubsetf asserts that the specified list(array, slice…) contains not all
elements given in the specified subset(array, slice…).

a.NotSubsetf([1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]", "error message %s", "formatted")

func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) bool

NotZero asserts that i is not the zero value for its type.

func (a *Assertions) NotZerof(i interface{}, msg string, args ...interface{}) bool

NotZerof asserts that i is not the zero value for its type.

func (a *Assertions) Panics(f PanicTestFunc, msgAndArgs ...interface{}) bool

Panics asserts that the code inside the specified PanicTestFunc panics.

a.Panics(func(){ GoCrazy() })

func (a *Assertions) PanicsWithError(errString string, f PanicTestFunc, msgAndArgs ...interface{}) bool

PanicsWithError asserts that the code inside the specified PanicTestFunc
panics, and that the recovered panic value is an error that satisfies the
EqualError comparison.

a.PanicsWithError("crazy error", func(){ GoCrazy() })

PanicsWithErrorf asserts that the code inside the specified PanicTestFunc
panics, and that the recovered panic value is an error that satisfies the
EqualError comparison.

a.PanicsWithErrorf("crazy error", func(){ GoCrazy() }, "error message %s", "formatted")

func (a *Assertions) PanicsWithValue(expected interface{}, f PanicTestFunc, msgAndArgs ...interface{}) bool

PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that
the recovered panic value equals the expected panic value.

a.PanicsWithValue("crazy error", func(){ GoCrazy() })

func (a *Assertions) PanicsWithValuef(expected interface{}, f PanicTestFunc, msg string, args ...interface{}) bool

PanicsWithValuef asserts that the code inside the specified PanicTestFunc panics, and that
the recovered panic value equals the expected panic value.

a.PanicsWithValuef("crazy error", func(){ GoCrazy() }, "error message %s", "formatted")

Panicsf asserts that the code inside the specified PanicTestFunc panics.

a.Panicsf(func(){ GoCrazy() }, "error message %s", "formatted")

func (a *Assertions) Positive(e interface{}, msgAndArgs ...interface{}) bool

Positive asserts that the specified element is positive

a.Positive(1)
a.Positive(1.23)

func (a *Assertions) Positivef(e interface{}, msg string, args ...interface{}) bool

Positivef asserts that the specified element is positive

a.Positivef(1, "error message %s", "formatted")
a.Positivef(1.23, "error message %s", "formatted")

func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool

Regexp asserts that a specified regexp matches a string.

a.Regexp(regexp.MustCompile("start"), "it's starting")
a.Regexp("start...$", "it's not starting")

func (a *Assertions) Regexpf(rx interface{}, str interface{}, msg string, args ...interface{}) bool

Regexpf asserts that a specified regexp matches a string.

a.Regexpf(regexp.MustCompile("start"), "it's starting", "error message %s", "formatted")
a.Regexpf("start...$", "it's not starting", "error message %s", "formatted")

func (a *Assertions) Same(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool

Same asserts that two pointers reference the same object.

a.Same(ptr1, ptr2)

Both arguments must be pointer variables. Pointer variable sameness is
determined based on the equality of both type and value.

func (a *Assertions) Samef(expected interface{}, actual interface{}, msg string, args ...interface{}) bool

Samef asserts that two pointers reference the same object.

a.Samef(ptr1, ptr2, "error message %s", "formatted")

Both arguments must be pointer variables. Pointer variable sameness is
determined based on the equality of both type and value.

func (a *Assertions) Subset(list interface{}, subset interface{}, msgAndArgs ...interface{}) bool

Subset asserts that the specified list(array, slice…) contains all
elements given in the specified subset(array, slice…).

a.Subset([1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]")

func (a *Assertions) Subsetf(list interface{}, subset interface{}, msg string, args ...interface{}) bool

Subsetf asserts that the specified list(array, slice…) contains all
elements given in the specified subset(array, slice…).

a.Subsetf([1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]", "error message %s", "formatted")

func (a *Assertions) True(value bool, msgAndArgs ...interface{}) bool

True asserts that the specified value is true.

a.True(myBool)

Truef asserts that the specified value is true.

a.Truef(myBool, "error message %s", "formatted")

WithinDuration asserts that the two times are within duration delta of each other.

a.WithinDuration(time.Now(), time.Now(), 10*time.Second)

WithinDurationf asserts that the two times are within duration delta of each other.

a.WithinDurationf(time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted")

WithinRange asserts that a time is within a time range (inclusive).

a.WithinRange(time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second))

WithinRangef asserts that a time is within a time range (inclusive).

a.WithinRangef(time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second), "error message %s", "formatted")

YAMLEq asserts that two YAML strings are equivalent.

YAMLEqf asserts that two YAML strings are equivalent.

func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) bool

Zero asserts that i is the zero value for its type.

func (a *Assertions) Zerof(i interface{}, msg string, args ...interface{}) bool

Zerof asserts that i is the zero value for its type.

type BoolAssertionFunc func(TestingT, bool, ...interface{}) bool

BoolAssertionFunc is a common function prototype when validating a bool value. Can be useful
for table driven tests.

t := &testing.T{} // provided by test

isOkay := func(x int) bool {
	return x >= 42
}

tests := []struct {
	name      string
	arg       int
	assertion BoolAssertionFunc
}{
	{"-1 is bad", -1, False},
	{"42 is good", 42, True},
	{"41 is bad", 41, False},
	{"45 is cool", 45, True},
}

for _, tt := range tests {
	t.Run(tt.name, func(t *testing.T) {
		tt.assertion(t, isOkay(tt.arg))
	})
}
Output:

CollectT implements the TestingT interface and collects all errors.

func (c *CollectT) Copy(t TestingT)

Copy copies the collected errors to the supplied t.

func (c *CollectT) Errorf(format string, args ...interface{})

Errorf collects the error.

func (c *CollectT) FailNow()

FailNow panics.

func (c *CollectT) Reset()

Reset clears the collected errors.

type Comparison func() (success bool)

Comparison is a custom function that returns true on success and false on failure

type ComparisonAssertionFunc func(TestingT, interface{}, interface{}, ...interface{}) bool

ComparisonAssertionFunc is a common function prototype when comparing two values. Can be useful
for table driven tests.

t := &testing.T{} // provided by test

adder := func(x, y int) int {
	return x + y
}

type args struct {
	x int
	y int
}

tests := []struct {
	name      string
	args      args
	expect    int
	assertion ComparisonAssertionFunc
}{
	{"2+2=4", args{2, 2}, 4, Equal},
	{"2+2!=5", args{2, 2}, 5, NotEqual},
	{"2+3==5", args{2, 3}, 5, Exactly},
}

for _, tt := range tests {
	t.Run(tt.name, func(t *testing.T) {
		tt.assertion(t, tt.expect, adder(tt.args.x, tt.args.y))
	})
}
Output:

type ErrorAssertionFunc func(TestingT, error, ...interface{}) bool

ErrorAssertionFunc is a common function prototype when validating an error value. Can be useful
for table driven tests.

t := &testing.T{} // provided by test

dumbParseNum := func(input string, v interface{}) error {
	return json.Unmarshal([]byte(input), v)
}

tests := []struct {
	name      string
	arg       string
	assertion ErrorAssertionFunc
}{
	{"1.2 is number", "1.2", NoError},
	{"1.2.3 not number", "1.2.3", Error},
	{"true is not number", "true", Error},
	{"3 is number", "3", NoError},
}

for _, tt := range tests {
	t.Run(tt.name, func(t *testing.T) {
		var x float64
		tt.assertion(t, dumbParseNum(tt.arg, &x))
	})
}
Output:

type PanicTestFunc func()

PanicTestFunc defines a func that should be passed to the assert.Panics and assert.NotPanics
methods, and represents a simple func that takes no arguments, and returns nothing.

type TestingT interface {
	Errorf(format string, args ...interface{})
}

TestingT is an interface wrapper around *testing.T

type ValueAssertionFunc func(TestingT, interface{}, ...interface{}) bool

ValueAssertionFunc is a common function prototype when validating a single value. Can be useful
for table driven tests.

t := &testing.T{} // provided by test

dumbParse := func(input string) interface{} {
	var x interface{}
	_ = json.Unmarshal([]byte(input), &x)
	return x
}

tests := []struct {
	name      string
	arg       string
	assertion ValueAssertionFunc
}{
	{"true is not nil", "true", NotNil},
	{"empty string is nil", "", Nil},
	{"zero is not nil", "0", NotNil},
	{"zero is zero", "0", Zero},
	{"false is zero", "false", Zero},
}

for _, tt := range tests {
	t.Run(tt.name, func(t *testing.T) {
		tt.assertion(t, dumbParse(tt.arg))
	})
}
Output:

Уровень сложности
Средний

Время на прочтение
6 мин

Количество просмотров 5.3K

Всем привет. Меня зовут Алексей Бурмистров, я senior Golang разработчик в Quadcode. В процессе разработки биллинга, мы столкнулись с различными типами ошибок, которые могут возникать во время выполнения программы. В данной статье я хочу поделиться нашим опытом в структурировании и обработке этих ошибок, а также представить подходы, которые мы применили для их эффективной обработки и диагностики. Наша основная цель заключается в создании понятных и легко обрабатываемых ошибок, которые гарантируют надежную работу биллинга.

Ошибки это один из самых важных аспектов любого языка программирования. То, как обрабатываются ошибки, влияет на приложения многими способами. То, как определяются ошибки в Golang, немного отличается от таких языков как Java, Python, Javascript. В Go ошибки – это значения.​

Свой тип ошибки

Весь код, связанный с обработкой ошибок, размещен в корне нашего приложения. Это сделано для предотвращения конфликтов со стандартным пакетом errors. Такой подход делает обработку ошибок приложения более явной и не лишает возможности использовать стандартную библиотеку без применения алиасов.

​Основой всего является тип Error– конкретное представление ошибки, который реализует стандартный интерфейс error. Он имеет несколько полей, некоторые из которых могут быть не заданы:

type errorCode string

// Коды ошибок приложения.
const (
 ...
ENOTFOUND errorCode = "not_found"
EINTERNAL errorCode = "internal"
...
)

type Error struct {
// Вложенная ошибка
Err error `json:"err"`
// Дополнительный контекст ошибки
Fields map[string]interface{}
// Код ошибки.
Code errorCode `json:"code"`
// Сообщение об шибке, которое понятно пользователю.
Message string `json:"message"`
// Выполняемая операция
Op string `json:"op"`
}
  • Op обозначает выполняемую операцию. Оно представляет собой строку, которая содержит имя метода или функции такие как repo.User, convert, Auth.Login и так далее.

  • Message содержит сообщение или ключ перевода ошибки, которое можно показать пользователю.

  • Code – это конкретный тип ошибки. Это обеспечивает стандартизацию и однозначность в обработке ошибок, позволяя легко идентифицировать и классифицировать ошибки. Список возможных типов может быть расширен при необходимости, обеспечивая гибкость и возможность добавления новых типов ошибок по мере развития всего приложения. Еще это позволяет нам точно определить статусы ответов API, связанные с каждым типом ошибки.

  • Fields представляет собой данные, связанные с ошибкой. Эти данные могут содержать идентификаторы объектов, параметры запросов или любую другую информацию, которая может быть полезной для понимания причины ошибки.

  • Err содержит вложенную ошибку, которая может быть связана с текущей ошибкой. Это может быть ошибка, возвращаемая внешней библиотекой или наша собственная Error

Вложенная ошибка полезна для отслеживания цепочки ошибок и построение трассировки, о которой мы поговорим ниже.

Создание ошибки

Для создания ошибки мы решили не делать отдельный конструктор, потому как структура не такая объемная. Для того чтобы разработчики не ошиблись при создании ошибки (например забыли & или не создали ошибку без Err и Message), мы используем собственный линтер для golangci-lint

Давайте рассмотрим пример. При обычном использовании мы можем вернуть ошибку в методе несколько раз, поэтому мы определим константу, условно называемую op, которая будет передаваться всем ошибкам в методе:

func (r *userRepository) User(ctx context.Context, id int) (*User, error) {
const op = "userRepository.User"
...
}

Еcли нам нужно только добавить op к ошибке для передачи на верхний уровень мы можем воспользоваться вспомогательными функциями OpError или OpErrorOrNil

...
var user User
err := db.QueryRow(ctx, query, id)
if err != nil {
 if errors.Is(err, pgx.ErrNoRows) {
  return nil, &app.Error{Op: op,Code: app.ENOTFOUND, Message: "user not found"}
 }
 return app.OpError(op, err)
}
...

Обработка ошибок​

Преимуществом собственного типа ошибки стала легкость, с которой мы могли писать тесты, зависящие от ошибок, а также писать чувствительный к ошибкам код вне тестов.

Для проверки Code есть вспомогательная функция ErrorCode которая возвращает код ошибки, если это была ошибка приложения, или EINTERNAL если это была другая.

switch ErrorCode(err) {
case ENOTFOUND:
...
case EINTERNAL:
...
}

Если нам нужен полный доступ к Error можно воспользоваться стандартной библиотекой errors.

appErr := &Error{}
if errors.As(err, appErr) {
 ...
}

Использование поля Code позволяет понятно преобразовывать ошибки в HTTP-статус. Для этого можно создать map, где ключами будут значения Code, а значениями соответствующие HTTP-статусы.

Примерное преобразования ошибки в HTTP-статус:

var codeToHTTPStatusMap = map[errorCode]int{
   ENOTFOUND: http.StatusNotFound,
   EINTERNAL: http.StatusInternalServerError,
   // Другие соответствия кодов ошибок и HTTP-статусов
}

func ErrCodeToHTTPStatus(err error) int {
   code := ErrorCode(err)
   if v, ok := codeToHTTPStatusMap[code]; ok {
       return v
   }
  
   // Возвращаем стандартный HTTP-статус для неизвестных ошибок
   return http.StatusInternalServerError
}

Теперь, чтобы получить соответствующий HTTP-статус для ошибки, достаточно вызвать функцию ErrCodeToHTTPStatus и передать ошибку ей. Она вернет соответствующий HTTP-статус. Если код ошибки не найден, будет возвращен стандартный HTTP-статус http.StatusInternalServerError.

Анализ и диагностика​

При анализе ошибок, возникающих в нашем приложении, мы полагаемся на информацию, которую мы записываем в логи. Однако, когда ошибки логируются в виде строк, это значительно затрудняет поиск и анализ. Так как мы структурируем логи для отправки в graylog, мы логируем ошибки в виде объектов, содержащих следующую информацию:

  • code: тип ошибки, чтобы понять её характер;

  • msg: сообщение из Err.Error();

  • fields: контекст, добавленный к ошибке;

  • trace: стек трассировки операций.

В наших логах мы избегаем логирования стандартного стека трассировки при ошибках приложения, поскольку он предоставляет недостаточно информации и его анализ затруднителен. Вот пример обычного стека трассировки:

goroutine 1 [running]:
testing.(*InternalExample).processRunResult(0xc000187aa8, {0x0, 0x0}, 0x0?, 0x0, {0x1043760e0, 0x1043b8d88})
       /opt/homebrew/Cellar/go/1.19.4/libexec/src/testing/example.go:91 +0x45c
testing.runExample.func2()
       /opt/homebrew/Cellar/go/1.19.4/libexec/src/testing/run_example.go:59 +0x14c
panic({0x1043760e0, 0x1043b8d88})
       /opt/homebrew/Cellar/go/1.19.4/libexec/src/runtime/panic.go:890 +0x258
app.foo(...)
       app/errors_test.go:336
app.bar()
       app/errors_test.go:341 +0x38
app.baz()
       app/errors_test.go:345 +0x24
app.ExampleTrace()
       app/errors_test.go:350 +0x24
testing.runExample({{0x1042f8cd5, 0xc}, 0x1043b8528, {0x1042fcab9, 0x19}, 0x0})
       /opt/homebrew/Cellar/go/1.19.4/libexec/src/testing/run_example.go:63 +0x2ec
testing.runExamples(0xc000187e00, {0x10450e080, 0x1, 0x0?})
       /opt/homebrew/Cellar/go/1.19.4/libexec/src/testing/example.go:44 +0x1ec
testing.(*M).Run(0xc00014a320)
       /opt/homebrew/Cellar/go/1.19.4/libexec/src/testing/testing.go:1728 +0x934
main.main()

В данном случае содержится много избыточной информации, которую сложно проанализировать. Однако стек трассировки операций выглядит следующим образом:

["ExampleRun", "baz", "bar", "foo"]

Стек трассировки операций легко читается и содержит только доменную логику, что является важным для нас.

Для логирования мы используем пакет go.uber.org/zap. Для него мы сделали вспомогательную функцию Error(err error) zap.Field, которая позволяет нам легко логировать ошибку в виде объекта.

Пример использования данной функции выглядит следующим образом:

func foo() {
   ...
   if err != nil {
       logger.Error("something gone wrong", Error(err))
   }
}

Пример вывода ошибки в логе может выглядеть следующим образом:

{"level":"error","msg":"something gone wrong","error":{"msg":"user not found","code":"not_found","trace":["userRepository.User"],"fields":{"user_id":"65535"}}}

Финальные выводы

В Golang у нас есть полная свобода выбора способа обработки ошибок в наших приложениях. Однако с этой свободой приходит и большая ответственность, так как правильная обработка ошибок играет ключевую роль в надежной работе приложения. Важно понимать, что каждое приложение имеет свои особенности, и мы можем модифицировать обработку ошибок в соответствии с конкретными требованиями и контекстом.

Мы можем гибко управлять данными, содержащимися в структуре Error, и модифицировать их в соответствии с нашими потребностями. Мы можем вносить корректировки, добавлять дополнительные данные и функциональность, чтобы улучшить отслеживаемость и анализ ошибок.

Если вы хотите ознакомиться с примерами кода, вы можете найти их в GitHub.

Грамотная обработка ошибок является важной составляющей разработки приложений и нужно постоянно стремиться улучшать подход к обработке и анализу ошибок, чтобы обеспечить более надежную и удобную работу приложений.

Go logo

Tutorials

Comprehensive Guide to Testing in Go

Read this post in other languages:

This article was written by an external contributor.

Alexandre Couëdelo

Alexandre Couëdelo

Alexandre is a complex systems engineering and management specialist. He has been embracing the DevOps culture since he started his career by contributing to the digital transformation of a leading financial institution in Canada. His passions are the DevOps revolution and industrial engineering, and he loves being able to get the best of both worlds.

GitHub Twitter

Testing is an essential part of the development process, and it’s a critical part of the software development life cycle. It ensures that your app functions correctly and meets your customer’s needs. This article will cover everything you need to know about Go testing. You will start with a simple testing function, and work through more tools and strategies to help you master testing in Go.

You’ll learn about a number of different modes of testing, such as table-driven tests, which are used to better organize your test cases; benchmark tests, which are used to validate performance; and fuzz tests, which allow you to explore edge cases and discover bugs.

You will also learn about tools from the standard testing package and its helper functions, and how code coverage can show you how much of your code is being tested. You’ll also learn about Testify, an assertion and mocking library that will improve test readability.

You can find all the code examples in this GitHub repository.

Writing a Simple Unit Test

Unit tests are a way to test small pieces of code, such as functions and methods. This is useful because it allows you to find bugs early. Unit tests make your testing strategies more efficient, since they are small and independent, and thus easy to maintain.

Let’s create an example to practice testing. Create a function, Fooer, that takes an int as input and returns a string . If the input integer is divisible by three, then return "Foo"; otherwise, return the number as a string.

You may recognize an oversimplified example of the FooBarQix coding question. Writing tests around this question will help you practice testing with Go.

Create a new file called fooer.go, and paste in the following code to create the example:

package main

import "strconv"

// If the number is divisible by 3, write "Foo" otherwise, the number
func Fooer(input int) string {

    isfoo := (input % 3) == 0

    if isfoo {
        return "Foo"
    }

    return strconv.Itoa(input)
}

Unit tests in Go are located in the same package (that is, the same folder) as the tested function. By convention, if your function is in the file fooer.go file, then the unit test for that function is in the file fooer_test.go.

Let’s write a simple function that tests your Fooer function, which returns "Foo" , when you input 3:

package main
import "testing"
func TestFooer(t *testing.T) {
	result := Fooer(3)
	if result != "Foo" {
	t.Errorf("Result was incorrect, got: %s, want: %s.", result, "Foo")
	}
}

A test function in Go starts with Test and takes *testing.T as the only parameter. In most cases, you will name the unit test Test[NameOfFunction]. The testing package provides tools to interact with the test workflow, such as t.Errorf, which indicates that the test failed by displaying an error message on the console.

You can run your tests using the command line:

go test

The output should look like this:

In GoLand, you can run a specific test by clicking on the green arrow in the gutter.

After GoLand finishes running your tests, it shows the results in the Run tool window. The console on the right shows the output of the current test session. It allows you to see the detailed information on the test execution and why your tests failed or were ignored.

To run all the tests in a package, click on the green double-triangle icon at the top.

Writing Table-Driven Tests

When writing tests, you may find yourself repeating a lot of code in order to cover all the cases required. Think about how you would go about covering the many cases involved in the Fooer example. You could write one test function per case, but this would lead to a lot of duplication. You could also call the tested function several times in the same test function and validate the output each time, but if the test fails, it can be difficult to identify the point of failure. Instead, you can use a table-driven approach to help reduce repetition. As the name suggests, this involves organizing a test case as a table that contains the inputs and the desired outputs.

This comes with two benefits:

  • Table tests reuse the same assertion logic, keeping your test DRY.
  • Table tests make it easy to know what is covered by a test, as you can easily see what inputs have been selected. Additionally, each row can be given a unique name to help identify what’s being tested and express the intent of the test.

Here is an example of a table-driven test function for the Fooer function:

func TestFooerTableDriven(t *testing.T) {
	  // Defining the columns of the table
		var tests = []struct {
		name string
			input int
			want  string
		}{
			// the table itself
			{"9 should be Foo", 9, "Foo"},
			{"3 should be Foo", 3, "Foo"},
			{"1 is not Foo", 1, "1"},
			{"0 should be Foo", 0, "Foo"},
		}
	  // The execution loop
		for _, tt := range tests {
			t.Run(tt.name, func(t *testing.T) {
				ans := Fooer(tt.input)
				if ans != tt.want {
					t.Errorf("got %s, want %s", ans, tt.want)
				}
			})
		}
	}

A table-driven test starts by defining the input structure. You can see this as defining the columns of the table. Each row of the table lists a test case to execute. Once the table is defined, you write the execution loop.

The execution loop calls t.Run(), which defines a subtest. As a result, each row of the table defines a subtest named [NameOfTheFuction]/[NameOfTheSubTest].

This way of writing tests is very popular, and considered the canonical way to write unit tests in Go. GoLand can generate those test templates for you. You can right-click your function and go to Generate | Test for function. Check out GoLand’s documentation for more details. 

All you need to do is add the test cases:

		{"9 should be Foo", args{9}, "Foo"},
		{"3 should be Foo", args{3}, "Foo"},
		{"1 is not Foo", args{1}, "1"},
		{"0 should be Foo", args{0}, "Foo"},

When you execute the test, you’ll see that your TestFooerTableDriven function runs four subtests, one for each row of the table.

With the generate feature, writing table-driven tests becomes simple and intuitive.

The Testing Package

The testing package plays a pivotal role in Go testing. It enables developers to create unit tests with different types of test functions. The testing.T type offers methods to control test execution, such as running tests in parallel with Parallel(), skipping tests with Skip(), and calling a test teardown function with Cleanup().

Errors and Logs

The testing.T type provides various practical tools to interact with the test workflow, including t.Errorf(), which prints out an error message and sets the test as failed.

It is important to mention that t.Error* does not stop the execution of the test. Instead, all encountered errors will be reported once the test is completed. Sometimes it makes more sense to fail the execution; in that case, you should use t.Fatal*. In some situations, using the Log*() function to print information during the test execution can be handy:

func TestFooer2(t *testing.T) {
			input := 3
			result := Fooer(3)
			t.Logf("The input was %d", input)
			if result != "Foo" {
				t.Errorf("Result was incorrect, got: %s, want: %s.", result, "Foo")
			}
			t.Fatalf("Stop the test now, we have seen enough")
			t.Error("This won't be executed")
		}

The output prompt should look like this:

As it’s shown, the last line t.Error("This won't be executed") has been skipped, because t.Fatalf has already terminated this test.

Running Parallel Tests

By default, tests are run sequentially; the method Parallel() signals that a test should be run in parallel. All tests calling this function will be executed in parallel. go test handles parallel tests by pausing each test that calls t.Parallel(), and then resuming them in parallel when all non-parallel tests have been completed. The GOMAXPROCS environment defines how many tests can run in parallel at one time, and by default this number is equal to the number of CPUs.

You can build a small example running two subtests in parallel. The following code will test Fooer(3) and Fooer(7) at the same time:

func TestFooerParallel(t *testing.T) {
		t.Run("Test 3 in Parallel", func(t *testing.T) {
			t.Parallel()
			result := Fooer(3)
			if result != "Foo" {
				t.Errorf("Result was incorrect, got: %s, want: %s.", result, "Foo")
			}
		})
		t.Run("Test 7 in Parallel", func(t *testing.T) {
			t.Parallel()
			result := Fooer(7)
			if result != "7" {
				t.Errorf("Result was incorrect, got: %s, want: %s.", result, "7")
			}
		})
	}

GoLand prints all status information (RUN, PAUSE, or CONT) for each test during the execution. When you run the above code, you can clearly see Test_3 was paused before Test_7 started to run. After Test_7 was paused, both tests were resumed and ran until finished.

To reduce duplication, you may want to use table-driven tests when using Parallel(). As you can see, this example required the duplication of some of the assertion logic.

Skipping Tests

Using the Skip() method allows you to separate unit tests from integration tests. Integration tests validate multiple functions and components together and are usually slower to execute, so sometimes it’s useful to execute unit tests only. For example, go test accepts a flag called -test.short that is intended to run a “fast” test. However, go test does not decide whether tests are “short” or not. You need to use a combination of testing.Short(), which is set to true when -short is used, and t.Skip(), as illustrated below:

func TestFooerSkiped(t *testing.T) {
		if testing.Short() {
			t.Skip("skipping test in short mode.")
		}
		result := Fooer(3)
		if result != "Foo" {
			t.Errorf("Result was incorrect, got: %s, want: %s.", result, "Foo")
		}
	}

This test will be executed if you run go test -v, but will be skipped if you run go test -v -test.short.

As shown below, the test was skipped in short mode.

Writing out the flags every time you run your tests can be tedious. Fortunately, GoLand allows you to save run/debug configurations for each test. A configuration is created every time you run a test using the green arrow in the gutter. 

You can read more about configuration templates for tests in GoLand’s Help. 

Test Teardown and Cleanup

The Cleanup() method is convenient for managing test tear down. At first glance, it may not be evident why you would need that function when you can use the defer keyword.

Using the defer solution looks like this:

func Test_With_Cleanup(t *testing.T) {

  // Some test code

  defer cleanup()

  // More test code
}

While this is simple enough, it comes with a few issues that are described in this article about the Go 1.14 new features. The main argument against the defer approach is that it can make test logic more complicated to set up, and can clutter the test function when many components are involved.

The Cleanup() function is executed at the end of each test (including subtests), and makes it clear to anyone reading the test what the intended behavior is.

func Test_With_Cleanup(t *testing.T) {

  // Some test code here

    t.Cleanup(func() {
        // cleanup logic
    })

  // more test code here

}

You can read more about the tests clean up with examples in this article. 

At that point, it is worth mentioning the Helper() method. This method exists to improve the logs when a test fails. In the logs, the line number of the helper function is ignored and only the line number of the failing test is reported, which helps figure out which test failed.

func helper(t *testing.T) {
  t.Helper()
  // do something
}

func Test_With_Cleanup(t *testing.T) {

  // Some test code here

    helper(t)

  // more test code here

}

Finally, TempDir() is a method that automatically creates a temporary directory for your test and deletes the folder when the test is completed, removing the need to write additional cleanup logic.

func TestFooerTempDir(t *testing.T) {
    tmpDir := t.TempDir()

  // your tests
}

This function is very practical, but due to its relative newness, many Go developers are unaware of it and still manage temporary directories manually in their tests.

Writing Coverage Tests

As tests are crucial to modern development work, it’s essential to know how much of your code is covered by tests. You can use Go’s built-in tool to generate a test report for the package you’re testing by simply adding the -cover flag in the test command:

go test -cover

Note that you can also add the flag -v for more detailed logs.

By default, test coverage calculates the percentage of statements covered through tests. In the output, you can see that eighty percent of the code is currently covered. (The main function is not covered by the tests.) While it’s complicated to fully explain how test coverage is calculated, you can read The Cover Story if you’re interested in knowing more about it.

There are various arguments that can be passed to go test -cover. For example, go test only considers packages with test files in the coverage calculation. You can use -​coverpkg to include all packages in the coverage calculation:

go test ./... -coverpkg=./...

You can find a working example in this GitHub repository expanding on why you would need -​coverpkg.

Using the flag -coverprofile will create a local coverage report file. This is useful when running tests in CI/CD, since you often send the report to your favorite code quality tool.

go test -coverprofile=output_filename

You can also use go tool cover to format the report. For example, the -html flag will open your default browser to display a graphical report.

go tool cover -html=output_filename

You can also simply use GoLand’s built-in coverage report. If you use the Run with Coverage option, you’ll get a detailed coverage report on the side panel.

GoLand also highlights covered lines in green, and uncovered lines in red. This is very helpful in deciding what test to write next. In the image below, some additional code was added to demonstrate what uncovered code looks like.

The last flag you need to know about is -covermode . By default, the coverage is calculated based on the statement coverage, but this can be changed to take into account how many times a statement is covered. There are several different options:

  • set: Coverage is based on statements.
  • count: Count is how many times a statement was run. It allows you to see which parts of code are only lightly covered.
  • atomic: Similar to count, but for parallel tests.

Knowing which flag to use when running coverage tests is most important when running tests in your CI/CD, because locally you can rely on GoLand for a friendly coverage report. Note that GoLand uses the atomic mode by default, which allows for coverage to be represented in shades of green on the left side of covered statements. 

Writing Benchmark Tests

Benchmark tests are a way of testing your code performance. The goal of those tests is to verify the runtime and the memory usage of an algorithm by running the same function many times.

To create a benchmark test:

  • Your test function needs to be in a *_test file.
  • The name of the function must start with Benchmark.
  • The function must accept *testing.B as the unique parameter.
  • The test function must contain a for loop using b.N as its upper bound.

Here is a simple example of a benchmark test of the Fooer function:

func BenchmarkFooer(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Fooer(i)
    }
}

The target code should be run N times in the benchmark function, and N is automatically adjusted at runtime until the execution time of each iteration is statistically stable.

In this example, the benchmark test ran 59,969,790 times with a speed of 19 ns per iteration. Benchmark tests themselves never fail.

You will have to use other tools if you want to save and analyze the results in your CI/CD. perf/cmd offers packages for this purpose. benchstat can be used to analyze the results, and benchsave can be used to save the result.

Writing Fuzz Tests

Fuzz testing is an exciting testing technique in which random input is used to discover bugs or edge cases. Go’s fuzzing algorithm is smart because it will try to cover as many statements in your code as possible by generating many new input combinations.

To create a fuzz test:

  • Your test function needs to be in a _test file.
  • The name of the function must start with Fuzz.
  • The test function must accept testing.F as the unique parameter.
  • The test function must define initial values, called seed corpus, with the f.Add() method.
  • The test function must define a fuzz target.

Let’s put all these into a comprehensive example:

func FuzzFooer(f *testing.F) {
    f.Add(3)
    f.Fuzz(func(t *testing.T, a int) {
        Fooer(a)
    })
}

The goal of the fuzz test is not to validate the output of the function, but instead to use unexpected inputs to find potential edge cases. By default, fuzzing will run indefinitely, as long as there isn’t a failure. The -fuzztime flag should be used in your CI/CD to specify the maximum time for which fuzzing should run. This approach to testing is particularly interesting when your code has many branches; even with table-driven tests, it can be hard to cover a large set of input, and fuzzing helps to solve this problem.

To run a fuzz test in GoLand, click on the green triangle in the gutter and select Run | go test -fuzz option, otherwise (without -fuzz) the test will only run once, using the corpus seed.

The Testify Package

Testify is a testing framework providing many tools for testing Go code. There is a considerable debate in the Go community about whether you should use Testify or just the standard library. Proponents feel that it increases the readability of the test and its output.

Testify can be installed with the command go get github.com/stretchr/testify.

Testify provides assert functions and mocks, which are similar to traditional testing frameworks, like JUnit for Java or Jasmine for NodeJS.

func TestFooerWithTestify(t *testing.T) {

    // assert equality
    assert.Equal(t, "Foo", Fooer(0), "0 is divisible by 3, should return Foo")

    // assert inequality
    assert.NotEqual(t, "Foo", Fooer(1), "1 is not divisible by 3, should not return Foo")
}

Testify provides two packages, require and assert. The require package will stop execution if there is a test failure, which helps you fail fast. assert lets you collect information, but accumulate the results of assertions.

func TestMapWithTestify(t *testing.T) {

    // require equality
    require.Equal(t, map[int]string{1: "1", 2: "2"}, map[int]string{1: "1", 2: "3"})

    // assert equality
    assert.Equal(t, map[int]string{1: "1", 2: "2"}, map[int]string{1: "1", 2: "2"})
}

When running the above test, all the lines below the first require assertion will be skipped.

The output log also clearly indicates the difference between the actual output and the expected output. Compared to Go’s built-in testing package, the output is more readable, especially when the testing data is complicated, such as with a long map or a complicated object. The log points out exactly which line is different, which can boost your productivity.

GoLand integrates with Testify to analyze the assertion result.

Wrapping Up

Testing is important because it allows you to validate the code’s logic and find bugs when changing code. Go offers numerous tools out of the box to test your application. You can write any test with the standard library, and Testify and its “better” assertion function and mock capability offer optional additional functionality.

The testing package offers three testing modes: regular tests (testing.T), benchmark tests (testing.B), and fuzz tests (testing.F). Setting any type of test is very simple. The testing package also offers many helper functions that will help you write better and cleaner tests. Spend some time exploring the library before jumping into testing.

Finally, your IDE plays a crucial role in writing tests efficiently and productively. We hope this article has allowed you to discover some of the features of GoLand, such as generating table-driven tests, running test functions with a click, the coverage panel, color-coded coverage gutters, and integration with Testify for easier debugging.

Subscribe to Blog updates

Discover more

Понравилась статья? Поделить с друзьями:
  • Godex g500 коды ошибок
  • Godex g300 ошибки
  • Golang обработка ошибок
  • Golang ошибка eof
  • God of war ошибка при запуске на пк