📖 15 min read (~ 3100 words).

Comparison

Comparing Ordered Values

Assertions

GoDoc

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

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

Greater

Greater asserts that the first element is strictly greater than the second.

Both elements must be of the same type in the reflect.Kind sense. To compare values that need a type conversion (e.g. float32 against float64), you need to convert types beforehand.

Examples
	assertions.Greater(t, 2, 1)
	assertions.Greater(t, float64(2), float64(1))
	assertions.Greater(t, "b", "a")
	success: 2, 1
	failure: 1, 2
  • Copy and click to open Go Playground

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

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

GreaterOrEqual

GreaterOrEqual asserts that the first element is greater than or equal to the second.

See also Greater.

Examples
	assertions.GreaterOrEqual(t, 2, 1)
	assertions.GreaterOrEqual(t, 2, 2)
	assertions.GreaterOrEqual(t, "b", "a")
	assertions.GreaterOrEqual(t, "b", "b")
	success: 2, 1
	failure: 1, 2
  • Copy and click to open Go Playground

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

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

GreaterOrEqualT[Orderable Ordered]

GreaterOrEqualT asserts that for two elements of the same type, the first element is greater than or equal to the second.

The Ordered type can be any of Go’s cmp.Ordered (strings, numeric types), []byte (uses bytes.Compare) and time.Time (uses time.Time.Compare.

Notice that pointers are not Ordered, but uintptr are. So you can’t call GreaterOrEqualT with [*time.Time].

GreaterOrEqualT ensures type safety at build time. If you need to compare values with a dynamically assigned type, use GreaterOrEqual instead.

To compare values that need a type conversion (e.g. float32 against float64), you need to convert types beforehand.

Examples
	assertions.GreaterOrEqualT(t, 2, 1)
	assertions.GreaterOrEqualT(t, 2, 2)
	assertions.GreaterOrEqualT(t, "b", "a")
	assertions.GreaterOrEqualT(t, "b", "b")
	success: 2, 1
	failure: 1, 2
  • Copy and click to open Go Playground

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

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

GreaterT[Orderable Ordered]

GreaterT asserts that for two elements of the same type, the first element is strictly greater than the second.

The Ordered type can be any of Go’s cmp.Ordered (strings, numeric types), []byte (uses bytes.Compare) and time.Time (uses time.Time.Compare.

Notice that pointers are not Ordered, but uintptr are. So you can’t call GreaterT with [*time.Time].

GreaterT ensures type safety at build time. If you need to compare values with a dynamically assigned type, use Greater instead.

To compare values that need a type conversion (e.g. float32 against float64), you need to convert types beforehand.

Examples
	assertions.GreaterT(t, 2, 1)
	assertions.GreaterT(t, float64(2), float64(1))
	assertions.GreaterT(t, "b", "a")
	assertions.GreaterT(t, time.Date(2026,1,1,0,0,0,0,nil), time.Now())
	success: 2, 1
	failure: 1, 2
  • Copy and click to open Go Playground

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

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

Less

Less asserts that the first element is strictly less than the second.

Both elements must be of the same type in the reflect.Kind sense. To compare values that need a type conversion (e.g. float32 against float64), you need to convert types beforehand.

Examples
	assertions.Less(t, 1, 2)
	assertions.Less(t, float64(1), float64(2))
	assertions.Less(t, "a", "b")
	success: 1, 2
	failure: 2, 1
  • Copy and click to open Go Playground

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

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

LessOrEqual

LessOrEqual asserts that the first element is less than or equal to the second.

Examples
	assertions.LessOrEqual(t, 1, 2)
	assertions.LessOrEqual(t, 2, 2)
	assertions.LessOrEqual(t, "a", "b")
	assertions.LessOrEqual(t, "b", "b")
	success: 1, 2
	failure: 2, 1
  • Copy and click to open Go Playground

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

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

LessOrEqualT[Orderable Ordered]

LessOrEqualT asserts that for two elements of the same type, the first element is less than or equal to the second.

The Ordered type can be any of Go’s cmp.Ordered (strings, numeric types), []byte (uses bytes.Compare) and time.Time (uses time.Time.Compare.

Notice that pointers are not Ordered, but uintptr are. So you can’t call LessOrEqualT with [*time.Time].

LessOrEqualT ensures type safety at build time. If you need to compare values with a dynamically assigned type, use LessOrEqual instead.

To compare values that need a type conversion (e.g. float32 against float64), you should use LessOrEqual instead.

Examples
	assertions.LessOrEqualT(t, 1, 2)
	assertions.LessOrEqualT(t, 2, 2)
	assertions.LessOrEqualT(t, "a", "b")
	assertions.LessOrEqualT(t, "b", "b")
	success: 1, 2
	failure: 2, 1
  • Copy and click to open Go Playground

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

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

LessT[Orderable Ordered]

LessT asserts that for two elements of the same type, the first element is strictly less than the second.

The Ordered type can be any of Go’s cmp.Ordered (strings, numeric types), []byte (uses bytes.Compare) and time.Time (uses time.Time.Compare.

Notice that pointers are not Ordered, but uintptr are. So you can’t call LessT with [*time.Time].

LessT ensures type safety at build time. If you need to compare values with a dynamically assigned type, use Less instead.

To compare values that need a type conversion (e.g. float32 against float64), you need to convert types beforehand.

Examples
	assertions.LessT(t, 1, 2)
	assertions.LessT(t, float64(1), float64(2))
	assertions.LessT(t, "a", "b")
	success: 1, 2
	failure: 2, 1
  • Copy and click to open Go Playground

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

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

Negative

Negative asserts that the specified element is strictly negative.

Examples
	assertions.Negative(t, -1)
	assertions.Negative(t, -1.23)
	success: -1
	failure: 1
  • Copy and click to open Go Playground

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

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

NegativeT[SignedNumber SignedNumeric]

NegativeT asserts that the specified element of a signed numeric type is strictly negative.

Examples
	assertions.NegativeT(t, -1)
	assertions.NegativeT(t, -1.23)
	success: -1
	failure: 1
  • Copy and click to open Go Playground

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

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

Positive

Positive asserts that the specified element is strictly positive.

Examples
	assertions.Positive(t, 1)
	assertions.Positive(t, 1.23)
	success: 1
	failure: -1
  • Copy and click to open Go Playground

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

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

PositiveT[SignedNumber SignedNumeric]

PositiveT asserts that the specified element of a signed numeric type is strictly positive.

Examples
	assertions.PositiveT(t, 1)
	assertions.PositiveT(t, 1.23)
	success: 1
	failure: -1
  • Copy and click to open Go Playground

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

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


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