Github.com/stretchr/testify: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 19: Line 19:


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


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


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


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


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


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

Revision as of 23:53, 10 January 2024

External

Internal

Overview

Installation

go get github.com/stretchr/testify

Programming Model

package yours

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

func TestSomething(t *testing.T) {

  assert := assert.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)
  }
}