📖 7 min read (~ 1400 words).

Condition

Expressing Assertions Using Conditions

Assertions

GoDoc

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

This domain exposes 5 functionalities. Generic assertions are marked with a . Their method variants carry a go1.27 badge: methods take type parameters only from go1.27 onwards, so on an older toolchain a generic assertion is available as a package-level function alone.

Blocked

Blocked asserts that a channel is blocked on receive.

It always fails if the operand is not a channel, or if the channel is send-only.

Examples
​
	ch := make(chan struct{})
	assertions.Blocked(t, ch)
	success:  make(chan struct{})
	failure:  sendChanMessage()
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestBlocked(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 TestBlocked(t *testing.T)
    	require.Blocked(t, make(chan struct{}))
    	fmt.Println("passed")
    
    }

BlockedT[E any, CHAN ~chan E]

BlockedT asserts that a channel is blocked on receive.

Examples
​
	ch := make(chan struct{})
	assertions.BlockedT(t, ch)
	success:  make(chan struct{})
	failure:  sendChanMessage()
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestBlockedT(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 TestBlockedT(t *testing.T)
    	require.BlockedT(t, make(chan struct{}))
    	fmt.Println("passed")
    
    }

Condition

Condition uses a comparison function to assert a complex condition.

Examples
​
	assertions.Condition(t, func() bool { return myCondition })
	success:  func() bool { return true }
	failure:  func() bool { return false }
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestCondition(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 TestCondition(t *testing.T)
    	require.Condition(t, func() bool {
    		return true
    	})
    	fmt.Println("passed")
    
    }

NotBlocked

NotBlocked asserts that a channel is not blocked on receive.

It always fails if the operand is not a channel, or if the channel is send-only.

A closed channel doesn’t block and returns true. Notice that this consumes any message available in the channel.

Examples
​
	ch := make(chan struct{}, 1)
	ch <- struct{}{}
	assertions.NotBlocked(t, ch)
	success:  sendChanMessage()
	failure:  make(chan struct{})
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotBlocked(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 TestNotBlocked(t *testing.T)
    	success := assert.NotBlocked(t, sendChanMessage())
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func sendChanMessage() chan struct{} {
    	ch := make(chan struct{}, 1)
    	ch <- struct{}{}
    
    	return ch
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotBlocked(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 TestNotBlocked(t *testing.T)
    	require.NotBlocked(t, sendChanMessage())
    	fmt.Println("passed")
    
    }
    
    func sendChanMessage() chan struct{} {
    	ch := make(chan struct{}, 1)
    	ch <- struct{}{}
    
    	return ch
    }

NotBlockedT[E any, CHAN ~chan E]

NotBlockedT asserts that a channel is not blocked on receive.

A closed channel doesn’t block and returns true. Notice that this consumes any message available in the channel.

Examples
​
	ch := make(chan struct{}, 1)
	ch <- struct{}{}
	assertions.NotBlockedT(t, ch)
	success:  sendChanMessage()
	failure:  make(chan struct{})
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotBlockedT(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 TestNotBlockedT(t *testing.T)
    	success := assert.NotBlockedT(t, sendChanMessage())
    	fmt.Printf("success: %t\n", success)
    
    }
    
    func sendChanMessage() chan struct{} {
    	ch := make(chan struct{}, 1)
    	ch <- struct{}{}
    
    	return ch
    }
  • Copy and click to open Go Playground

    // real-world test would inject *testing.T from TestNotBlockedT(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 TestNotBlockedT(t *testing.T)
    	require.NotBlockedT(t, sendChanMessage())
    	fmt.Println("passed")
    
    }
    
    func sendChanMessage() chan struct{} {
    	ch := make(chan struct{}, 1)
    	ch <- struct{}{}
    
    	return ch
    }


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