📖 19 min read (~ 4100 words).

Equality

Asserting Two Things Are Equal

Assertions

GoDoc

All links point to https://pkg.go.dev/github.com/go-openapi/testify/v2

This domain exposes 16 functionalities. Generic assertions are marked with a .

Empty

Empty asserts that the given value is “empty”.

Zero values are “empty”.

Arrays are “empty” if every element is the zero value of the type (stricter than “empty”).

Slices, maps and channels with zero length are “empty”.

Pointer values are “empty” if the pointer is nil or if the pointed value is “empty”.

Examples
	assertions.Empty(t, obj)
	success: ""
	failure: "not empty"
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEmpty(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEmpty(t *testing.T)
    	success := assert.Empty(t, "")
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEmpty(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEmpty(t *testing.T)
    	require.Empty(t, "")
    	fmt.Println("passed")
    
    }

Equal

Equal asserts that two objects are equal.

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.

Examples
	assertions.Equal(t, 123, 123)
	success: 123, 123
	failure: 123, 456
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEqual(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEqual(t *testing.T)
    	success := assert.Equal(t, 123, 123)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEqual(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEqual(t *testing.T)
    	require.Equal(t, 123, 123)
    	fmt.Println("passed")
    
    }

EqualExportedValues

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.

Function equality cannot be determined and will always fail.

Examples
	type S struct {
		Exported     	int
		notExported   	int
	}
	assertions.EqualExportedValues(t, S{1, 2}, S{1, 3}) => true
	assertions.EqualExportedValues(t, S{1, 2}, S{2, 3}) => false
	success: &dummyStruct{A: "a", b: 1}, &dummyStruct{A: "a", b: 2}
	failure: &dummyStruct{A: "a", b: 1}, &dummyStruct{A: "b", b: 1}
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEqualExportedValues(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEqualExportedValues(t *testing.T)
    	success := assert.EqualExportedValues(t, &dummyStruct{A: "a", b: 1}, &dummyStruct{A: "a", b: 2})
    	fmt.Printf("success: %t\n", success)
    
    }
    
    type dummyStruct struct {
    	A string
    	b int
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEqualExportedValues(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEqualExportedValues(t *testing.T)
    	require.EqualExportedValues(t, &dummyStruct{A: "a", b: 1}, &dummyStruct{A: "a", b: 2})
    	fmt.Println("passed")
    
    }
    
    type dummyStruct struct {
    	A string
    	b int
    }

EqualT[V comparable]

EqualT asserts that two objects of the same comparable type are equal.

Pointer variable equality is determined based on the equality of the memory addresses (unlike Equal, but like Same).

Functions, slices and maps are not comparable. See also ComparisonOperators.

If you need to compare values of non-comparable types, or compare pointers by the value they point to, use Equal instead.

Examples
	assertions.EqualT(t, 123, 123)
	success: 123, 123
	failure: 123, 456
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEqualT(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEqualT(t *testing.T)
    	success := assert.EqualT(t, 123, 123)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEqualT(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEqualT(t *testing.T)
    	require.EqualT(t, 123, 123)
    	fmt.Println("passed")
    
    }

EqualValues

EqualValues asserts that two objects are equal or convertible to the larger type and equal.

Function equality cannot be determined and will always fail.

Examples
	assertions.EqualValues(t, uint32(123), int32(123))
	success: uint32(123), int32(123)
	failure: uint32(123), int32(456)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEqualValues(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEqualValues(t *testing.T)
    	success := assert.EqualValues(t, uint32(123), int32(123))
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestEqualValues(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestEqualValues(t *testing.T)
    	require.EqualValues(t, uint32(123), int32(123))
    	fmt.Println("passed")
    
    }

Exactly

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

Examples
	assertions.Exactly(t, int32(123), int64(123))
	success: int32(123), int32(123)
	failure: int32(123), int64(123)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestExactly(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestExactly(t *testing.T)
    	success := assert.Exactly(t, int32(123), int32(123))
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestExactly(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestExactly(t *testing.T)
    	require.Exactly(t, int32(123), int32(123))
    	fmt.Println("passed")
    
    }

Nil

Nil asserts that the specified object is nil.

Examples
	assertions.Nil(t, err)
	success: nil
	failure: "not nil"
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNil(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNil(t *testing.T)
    	success := assert.Nil(t, nil)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNil(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNil(t *testing.T)
    	require.Nil(t, nil)
    	fmt.Println("passed")
    
    }

NotEmpty

NotEmpty asserts that the specified object is NOT Empty.

Examples
	if assert.NotEmpty(t, obj) {
		assertions.Equal(t, "two", obj[1](https://pkg.go.dev/github.com/go-openapi/testify/v2/assert#1))
	}
	success: "not empty"
	failure: ""
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotEmpty(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotEmpty(t *testing.T)
    	success := assert.NotEmpty(t, "not empty")
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotEmpty(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotEmpty(t *testing.T)
    	require.NotEmpty(t, "not empty")
    	fmt.Println("passed")
    
    }

NotEqual

NotEqual asserts that the specified values are NOT equal.

Examples
	assertions.NotEqual(t, obj1, obj2)
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.
	success: 123, 456
	failure: 123, 123
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotEqual(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotEqual(t *testing.T)
    	success := assert.NotEqual(t, 123, 456)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotEqual(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotEqual(t *testing.T)
    	require.NotEqual(t, 123, 456)
    	fmt.Println("passed")
    
    }

NotEqualT[V comparable]

NotEqualT asserts that the specified values of the same comparable type are NOT equal.

See EqualT.

Examples
	assertions.NotEqualT(t, obj1, obj2)
	success: 123, 456
	failure: 123, 123
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotEqualT(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotEqualT(t *testing.T)
    	success := assert.NotEqualT(t, 123, 456)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotEqualT(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotEqualT(t *testing.T)
    	require.NotEqualT(t, 123, 456)
    	fmt.Println("passed")
    
    }

NotEqualValues

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

Function equality cannot be determined and will always fail.

Examples
	assertions.NotEqualValues(t, obj1, obj2)
	success: uint32(123), int32(456)
	failure: uint32(123), int32(123)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotEqualValues(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotEqualValues(t *testing.T)
    	success := assert.NotEqualValues(t, uint32(123), int32(456))
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotEqualValues(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotEqualValues(t *testing.T)
    	require.NotEqualValues(t, uint32(123), int32(456))
    	fmt.Println("passed")
    
    }

NotNil

NotNil asserts that the specified object is not nil.

Examples
	assertions.NotNil(t, err)
	success: "not nil"
	failure: nil
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotNil(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotNil(t *testing.T)
    	success := assert.NotNil(t, "not nil")
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotNil(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotNil(t *testing.T)
    	require.NotNil(t, "not nil")
    	fmt.Println("passed")
    
    }

NotSame

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

See Same.

Examples
	assertions.NotSame(t, ptr1, ptr2)
	success: &staticVar, ptr("static string")
	failure: &staticVar, staticVarPtr
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotSame(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotSame(t *testing.T)
    	success := assert.NotSame(t, &staticVar, ptr("static string"))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    //nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
    var staticVar = "static string"
    
    func ptr[T any](value T) *T {
    	p := value
    
    	return &p
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotSame(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotSame(t *testing.T)
    	require.NotSame(t, &staticVar, ptr("static string"))
    	fmt.Println("passed")
    
    }
    
    //nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
    var staticVar = "static string"
    
    func ptr[T any](value T) *T {
    	p := value
    
    	return &p
    }

NotSameT[P any]

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

See SameT.

Examples
	assertions.NotSameT(t, ptr1, ptr2)
	success: &staticVar, ptr("static string")
	failure: &staticVar, staticVarPtr
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotSameT(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotSameT(t *testing.T)
    	success := assert.NotSameT(t, &staticVar, ptr("static string"))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    //nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
    var staticVar = "static string"
    
    func ptr[T any](value T) *T {
    	p := value
    
    	return &p
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotSameT(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestNotSameT(t *testing.T)
    	require.NotSameT(t, &staticVar, ptr("static string"))
    	fmt.Println("passed")
    
    }
    
    //nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
    var staticVar = "static string"
    
    func ptr[T any](value T) *T {
    	p := value
    
    	return &p
    }

Same

Same asserts that two pointers reference the same object.

Both arguments must be pointer variables.

Pointer variable sameness is determined based on the equality of both type and value.

Unlike Equal pointers, Same pointers point to the same memory address.

Examples
	assertions.Same(t, ptr1, ptr2)
	success: &staticVar, staticVarPtr
	failure: &staticVar, ptr("static string")
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestSame(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestSame(t *testing.T)
    	success := assert.Same(t, &staticVar, staticVarPtr)
    	fmt.Printf("success: %t\n", success)
    
    }
    
    //nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
    var (
    	staticVar    = "static string"
    	staticVarPtr = &staticVar
    )
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestSame(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestSame(t *testing.T)
    	require.Same(t, &staticVar, staticVarPtr)
    	fmt.Println("passed")
    
    }
    
    //nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
    var (
    	staticVar    = "static string"
    	staticVarPtr = &staticVar
    )

SameT[P any]

SameT asserts that two pointers of the same type reference the same object.

See Same.

Examples
	assertions.SameT(t, ptr1, ptr2)
	success: &staticVar, staticVarPtr
	failure: &staticVar, ptr("static string")
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestSameT(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/assert"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestSameT(t *testing.T)
    	success := assert.SameT(t, &staticVar, staticVarPtr)
    	fmt.Printf("success: %t\n", success)
    
    }
    
    //nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
    var (
    	staticVar    = "static string"
    	staticVarPtr = &staticVar
    )
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestSameT(t *testing.T)
    package main
    
    import (
    	"fmt"
    	"testing"
    
    	"github.com/go-openapi/testify/v2/require"
    )
    
    func main() {
    	t := new(testing.T) // should come from testing, e.g. func TestSameT(t *testing.T)
    	require.SameT(t, &staticVar, staticVarPtr)
    	fmt.Println("passed")
    
    }
    
    //nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
    var (
    	staticVar    = "static string"
    	staticVarPtr = &staticVar
    )


Generated with github.com/go-openapi/testify/codegen/v2