📖 13 min read (~ 2800 words).

Number

Asserting Numbers

Assertions

GoDoc

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

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

InDelta

InDelta asserts that the two numerals are within delta of each other.

Delta must be greater than or equal to zero.

Expected and actual values should convert to float64. To compare large integers that can’t be represented accurately as float64 (e.g. uint64), prefer InDeltaT to preserve the original type.

Behavior with IEEE floating point arithmetic

  • expected NaN is matched only by a NaN, e.g. this works: [InDeltaT](math.Sqrt, math.Sqrt, 0.0)
  • expected +Inf is matched only by a +Inf
  • expected -Inf is matched only by a -Inf
Examples
	assertions.InDelta(t, math.Pi, 22/7.0, 0.01)
	success: 1.0, 1.01, 0.02
	failure: 1.0, 1.1, 0.05
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestInDelta(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 TestInDelta(t *testing.T)
    	require.InDelta(t, 1.0, 1.01, 0.02)
    	fmt.Println("passed")
    
    }

InDeltaMapValues

InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.

See InDelta.

Examples
	assertions.InDeltaMapValues(t, map[string](https://pkg.go.dev/github.com/go-openapi/testify/v2/assert#string)float64{"a": 1.0}, map[string](https://pkg.go.dev/github.com/go-openapi/testify/v2/assert#string)float64{"a": 1.01}, 0.02)
	success: map[string](https://pkg.go.dev/github.com/go-openapi/testify/v2/assert#string)float64{"a": 1.0}, map[string](https://pkg.go.dev/github.com/go-openapi/testify/v2/assert#string)float64{"a": 1.01}, 0.02
	failure: map[string](https://pkg.go.dev/github.com/go-openapi/testify/v2/assert#string)float64{"a": 1.0}, map[string](https://pkg.go.dev/github.com/go-openapi/testify/v2/assert#string)float64{"a": 1.1}, 0.05
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestInDeltaMapValues(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 TestInDeltaMapValues(t *testing.T)
    	success := assert.InDeltaMapValues(t, map[string]float64{"a": 1.0}, map[string]float64{"a": 1.01}, 0.02)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestInDeltaMapValues(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 TestInDeltaMapValues(t *testing.T)
    	require.InDeltaMapValues(t, map[string]float64{"a": 1.0}, map[string]float64{"a": 1.01}, 0.02)
    	fmt.Println("passed")
    
    }

InDeltaSlice

InDeltaSlice is the same as InDelta, except it compares two slices.

See InDelta.

Examples
	assertions.InDeltaSlice(t, []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02)
	success: []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02
	failure: []float64{1.0, 2.0}, []float64{1.1, 2.1}, 0.05
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestInDeltaSlice(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 TestInDeltaSlice(t *testing.T)
    	success := assert.InDeltaSlice(t, []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestInDeltaSlice(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 TestInDeltaSlice(t *testing.T)
    	require.InDeltaSlice(t, []float64{1.0, 2.0}, []float64{1.01, 2.01}, 0.02)
    	fmt.Println("passed")
    
    }

InDeltaT[Number Measurable]

InDeltaT asserts that the two numerals of the same type numerical type are within delta of each other.

InDeltaT accepts any go numeric type, including integer types.

The main difference with InDelta is that the delta is expressed with the same type as the values, not necessarily a float64.

Delta must be greater than or equal to zero.

Behavior with IEEE floating point arithmetic

  • expected NaN is matched only by a NaN, e.g. this works: InDeltaT(math.NaN, math.Sqrt, 0.0)
  • expected +Inf is matched only by a +Inf
  • expected -Inf is matched only by a -Inf
Examples
	assertions.InDeltaT(t, math.Pi, 22/7.0, 0.01)
	success: 1.0, 1.01, 0.02
	failure: 1.0, 1.1, 0.05
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestInDeltaT(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 TestInDeltaT(t *testing.T)
    	require.InDeltaT(t, 1.0, 1.01, 0.02)
    	fmt.Println("passed")
    
    }

InEpsilon

InEpsilon asserts that expected and actual have a relative error less than epsilon.

Behavior with IEEE floating point arithmetic

  • expected NaN is matched only by a NaN, e.g. this works: [InDeltaT](math.NaN, math.Sqrt, 0.0)
  • expected +Inf is matched only by a +Inf
  • expected -Inf is matched only by a -Inf

Edge case: for very large integers that do not convert accurately to a float64 (e.g. uint64), prefer InDeltaT.

Formula:

  • If expected == 0: fail if |actual - expected| > epsilon
  • If expected != 0: fail if |actual - expected| > epsilon * |expected|

This allows InEpsilonT to work naturally across the full numeric range including zero.

Examples
	assertions.InEpsilon(t, 100.0, 101.0, 0.02)
	success: 100.0, 101.0, 0.02
	failure: 100.0, 110.0, 0.05
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestInEpsilon(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 TestInEpsilon(t *testing.T)
    	require.InEpsilon(t, 100.0, 101.0, 0.02)
    	fmt.Println("passed")
    
    }

InEpsilonSlice

InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices.

See InEpsilon.

Examples
	assertions.InEpsilonSlice(t, []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02)
	success: []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02
	failure: []float64{100.0, 200.0}, []float64{110.0, 220.0}, 0.05
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestInEpsilonSlice(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 TestInEpsilonSlice(t *testing.T)
    	success := assert.InEpsilonSlice(t, []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02)
    	fmt.Printf("success: %t\n", success)
    
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestInEpsilonSlice(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 TestInEpsilonSlice(t *testing.T)
    	require.InEpsilonSlice(t, []float64{100.0, 200.0}, []float64{101.0, 202.0}, 0.02)
    	fmt.Println("passed")
    
    }

InEpsilonSymmetric

InEpsilonSymmetric asserts that 2 numbers are close, with a symmetric relative error.

Unlike with InEpsilon, both numbers play a symmetric role and the relative error is computed relative to the number with greatest amplitude. This mirrors the behavior of Python’s math.isclose (with the relative-tolerance term only).

See also InEpsilon.

Behavior with IEEE floating point arithmetic

  • NaN is matched only by a NaN, e.g. this works: [InEpsilonSymmetric](math.NaN, math.Sqrt, 0.0)
  • +Inf is matched only by a +Inf
  • -Inf is matched only by a -Inf

Edge case: for very large integers that do not convert accurately to a float64 (e.g. uint64), prefer InDelta.

Formula:

  • If x == 0 and y == 0: success
  • Otherwise fail if |x - y| > epsilon * max(|x|,|y|)
Examples
	assertions.InEpsilonSymmetric(t, 100.0, 101.0, 0.02)
	success: 100.0, 101.0, 0.02
	failure: 100.0, 110.0, 0.05
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestInEpsilonSymmetric(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 TestInEpsilonSymmetric(t *testing.T)
    	require.InEpsilonSymmetric(t, 100.0, 101.0, 0.02)
    	fmt.Println("passed")
    
    }

InEpsilonSymmetricT[Number Measurable]

InEpsilonSymmetricT is the type-safe version of InEpsilonSymmetric, comparing numbers of the same numerical type.

See InEpsilonSymmetric.

Examples
	assertions.InEpsilonSymmetricT(t, 100.0, 101.0, 0.02)
	success: 100.0, 101.0, 0.02
	failure: 100.0, 110.0, 0.05
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestInEpsilonSymmetricT(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 TestInEpsilonSymmetricT(t *testing.T)
    	require.InEpsilonSymmetricT(t, 100.0, 101.0, 0.02)
    	fmt.Println("passed")
    
    }

InEpsilonT[Number Measurable]

InEpsilonT asserts that expected and actual have a relative error less than epsilon.

When expected is zero, epsilon is interpreted as an absolute error threshold, since relative error is mathematically undefined for zero values.

Unlike InDeltaT, which preserves the original type, InEpsilonT converts the expected and actual numbers to float64, since the relative error doesn’t make sense as an integer.

Behavior with IEEE floating point arithmetic

  • expected NaN is matched only by a NaN, e.g. this works: [InDeltaT](math.NaN, math.Sqrt, 0.0)
  • expected +Inf is matched only by a +Inf
  • expected -Inf is matched only by a -Inf

Edge case: for very large integers that do not convert accurately to a float64 (e.g. uint64), prefer InDeltaT.

Formula:

  • If expected == 0: fail if |actual - expected| > epsilon
  • If expected != 0: fail if |actual - expected| > epsilon * |expected|

This allows InEpsilonT to work naturally across the full numeric range including zero.

Examples
	assertions.InEpsilon(t, 100.0, 101.0, 0.02)
	success: 100.0, 101.0, 0.02
	failure: 100.0, 110.0, 0.05
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestInEpsilonT(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 TestInEpsilonT(t *testing.T)
    	require.InEpsilonT(t, 100.0, 101.0, 0.02)
    	fmt.Println("passed")
    
    }


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