Testify require and assert: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 8: Line 8:
<code>require</code> and <code>assert</code> packages provide functions to evaluate arguments and establish whether the test passes or fails.  
<code>require</code> and <code>assert</code> packages provide functions to evaluate arguments and establish whether the test passes or fails.  


The major difference between <code>require</code> and <code>assert</code> is that <code>assert</code> means '''continue on error'''. If an assertion fails, the test reports it and '''continues'''. In the end, the test is accounted as "failed", but it keeps going to the end. This is different from the behavior of <code>assert</code> in other languages and frameworks, where the first failed assertion stops the current test. This does not make too much sense - if an assertion is broken, I am not that interested in what comes after that, they are likely to be broken too, or at least, not trustworthy.
The major difference between <code>require</code> and <code>assert</code> is that <code>assert</code> means '''continue on error'''. If an assertion fails, the test reports it and '''continues'''. In the end, the test is accounted as "failed", but it keeps going to the end. This is different from the behavior of <code>assert</code> in other languages and frameworks, where the first failed assertion stops the current test. This does not make too much sense - if an assertion is broken, I am not that interested in what comes after that, they are likely to be broken too, or at least, not trustworthy. If you want the test to exit after the first failed assertion, use <code>require</code>. <code>require</code> means '''exit on error'''.


Internally, this is implemented by assert <code>calling</code> <code>Fail()</code> and <code>require</code> calling <code>FailNow()</code>.
Internally, this is implemented by assert <code>calling</code> <code>Fail()</code> and <code>require</code> calling <code>FailNow()</code>.

Revision as of 17:54, 12 March 2024

External

Internal

Overview

require and assert packages provide functions to evaluate arguments and establish whether the test passes or fails.

The major difference between require and assert is that assert means continue on error. If an assertion fails, the test reports it and continues. In the end, the test is accounted as "failed", but it keeps going to the end. This is different from the behavior of assert in other languages and frameworks, where the first failed assertion stops the current test. This does not make too much sense - if an assertion is broken, I am not that interested in what comes after that, they are likely to be broken too, or at least, not trustworthy. If you want the test to exit after the first failed assertion, use require. require means exit on error.

Internally, this is implemented by assert calling Fail() and require calling FailNow().

Equality and Non-Equality

In case the result is failure, Equal() and NotEqual() delegate to Fail().

package somepkg

import (
	"testing"

	testifyassert "github.com/stretchr/testify/assert"
)

func TestSomething(t *testing.T) {
	assert := testifyassert.New(t)
	assert.Equal(123, 123, "this message is displayed when the test fails %s", "because of inequality")
	assert.NotEqual(123, 321, "this message is displayed when the test fails %s", "because of equality")
}

Nil and Non-Nil

In case the result is failure, Nil() and NotNil() delegate to Fail().

package somepkg

import (
	"testing"

	testifyassert "github.com/stretchr/testify/assert"
)

func TestSomething(t *testing.T) {
	assert := testifyassert.New(t)
	someObj := &struct{}{}
	assert.NotNil(someObj, "someObj should not have been nil but it is %v", someObj)
	someObj = nil
	assert.Nil(someObj, "someObj should have been nil but it is %v", someObj)
}

Failing the Test

Fail()

Failf()

FailNow()

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