📖 11 min read (~ 2400 words).

Type

Asserting Types Rather Than Values

Assertions

GoDoc

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

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

Implements

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

Examples
	assertions.Implements(t, (*MyInterface)(nil), new(MyObject))
	success: ptr(dummyInterface), new(testing.T)
	failure: (*error)(nil), new(testing.T)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestImplements(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 TestImplements(t *testing.T)
    	success := assert.Implements(t, ptr(dummyInterface), new(testing.T))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    //nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
    var (
    	staticVar = "static string"
    
    	dummyInterface assert.T
    )
    
    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 TestImplements(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 TestImplements(t *testing.T)
    	require.Implements(t, ptr(dummyInterface), new(testing.T))
    	fmt.Println("passed")
    
    }
    
    //nolint:gochecknoglobals // this is on purpose to share a common pointer when testing
    var (
    	staticVar = "static string"
    
    	dummyInterface require.T
    )
    
    func ptr[T any](value T) *T {
    	p := value
    
    	return &p
    }

IsNotOfTypeT[EType any]

IsNotOfTypeT asserts that an object is not of a given type.

Examples
	assertions.IsOfType[MyType](t,myVar)
	success: 123.123
	failure: myType(123.123)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestIsNotOfTypeT(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 TestIsNotOfTypeT(t *testing.T)
    	success := assert.IsNotOfTypeT[myType](t, 123.123)
    	fmt.Printf("success: %t\n", success)
    
    }
    
    type myType float64
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestIsNotOfTypeT(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 TestIsNotOfTypeT(t *testing.T)
    	require.IsNotOfTypeT[myType](t, 123.123)
    	fmt.Println("passed")
    
    }
    
    type myType float64

IsNotType

IsNotType asserts that the specified objects are not of the same type.

Examples
	assertions.IsNotType(t, &NotMyStruct{}, &MyStruct{})
	success: int32(123), int64(456)
	failure: 123, 456
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestIsNotType(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 TestIsNotType(t *testing.T)
    	success := assert.IsNotType(t, int32(123), int64(456))
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestIsNotType(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 TestIsNotType(t *testing.T)
    	require.IsNotType(t, int32(123), int64(456))
    	fmt.Println("passed")
    
    }

IsOfTypeT[EType any]

IsOfTypeT asserts that an object is of a given type.

Examples
	assertions.IsOfTypeT[MyType](t,myVar)
	success: myType(123.123)
	failure: 123.123
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestIsOfTypeT(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 TestIsOfTypeT(t *testing.T)
    	success := assert.IsOfTypeT[myType](t, myType(123.123))
    	fmt.Printf("success: %t\n", success)
    
    }
    
    type myType float64
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestIsOfTypeT(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 TestIsOfTypeT(t *testing.T)
    	require.IsOfTypeT[myType](t, myType(123.123))
    	fmt.Println("passed")
    
    }
    
    type myType float64

IsType

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

Examples
	assertions.IsType(t, &MyStruct{}, &MyStruct{})
	success: 123, 456
	failure: int32(123), int64(456)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestIsType(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 TestIsType(t *testing.T)
    	success := assert.IsType(t, 123, 456)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestIsType(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 TestIsType(t *testing.T)
    	require.IsType(t, 123, 456)
    	fmt.Println("passed")
    
    }

Kind

Kind asserts that the reflect.Kind of a given object matches the expected reflect.Kind.

Kind reflects the concrete value stored in the object. The nil value (or interface with nil value) are comparable to reflect.Invalid. See also reflect.Value.Kind.

Examples
	assertions.Kind(t, reflect.String, "Hello World")
	success: reflect.String, "hello"
	failure: reflect.String, 0
  • Copy and click to open Go Playground

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

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

NotImplements

NotImplements asserts that an object does not implement the specified interface.

Examples
	assertions.NotImplements(t, (*MyInterface)(nil), new(MyObject))
	success: (*error)(nil), new(testing.T)
	failure: ptr(dummyInterface), new(testing.T)
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotImplements(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 TestNotImplements(t *testing.T)
    	success := assert.NotImplements(t, (*error)(nil), new(testing.T))
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotImplements(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 TestNotImplements(t *testing.T)
    	require.NotImplements(t, (*error)(nil), new(testing.T))
    	fmt.Println("passed")
    
    }

NotKind

NotKind asserts that the reflect.Kind of a given object does not match the expected reflect.Kind.

Kind reflects the concrete value stored in the object. The nil value (or interface with nil value) are comparable to reflect.Invalid. See also reflect.Value.Kind.

Examples
	assertions.NotKind(t, reflect.Int, "Hello World")
	success: reflect.String, 0
	failure: reflect.String, "hello"
  • Copy and click to open Go Playground

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

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

NotZero

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

Examples
	assertions.NotZero(t, obj)
	success: 1
	failure: 0
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotZero(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 TestNotZero(t *testing.T)
    	success := assert.NotZero(t, 1)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotZero(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 TestNotZero(t *testing.T)
    	require.NotZero(t, 1)
    	fmt.Println("passed")
    
    }

Zero

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

Examples
	assertions.Zero(t, obj)
	success: 0
	failure: 1
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestZero(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 TestZero(t *testing.T)
    	success := assert.Zero(t, 0)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestZero(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 TestZero(t *testing.T)
    	require.Zero(t, 0)
    	fmt.Println("passed")
    
    }


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