Testify require and assert

From NovaOrdis Knowledge Base
Revision as of 00:47, 12 March 2024 by Ovidiu (talk | contribs) (Created page with "=Internal= * Testify =Overview= =Failing the Test= ==Failing a Test from a Goroutine== <syntaxhighlight lang='go'> package yours import ( "testing" tassert "github.com/stretchr/testify/assert" ) func TestSomething(t *testing.T) { assert := tassert.New(t) // assert equality assert.Equal(123, 123, "they should be equal") // assert inequality assert.NotEqual(123, 456, "they should not be equal") // assert f...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Internal

Overview

Failing the Test

Failing a Test from a Goroutine

package yours

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

func TestSomething(t *testing.T) {

  assert := tassert.New(t)

  // assert equality
  assert.Equal(123, 123, "they should be equal")

  // assert inequality
  assert.NotEqual(123, 456, "they should not be equal")

  // assert for nil (good for errors)
  assert.Nil(object)

  // assert for not nil (good when you expect something)
  if assert.NotNil(object) {

    // now we know that object isn't nil, we are safe to make
    // further assertions without causing any errors
    assert.Equal("Something", object.Value)
  }
}

To check that an error has the expected message:

err := ...
assert.NotNil(err)
assert.Equal(err.Error(), "expected message")

Failing the Test