📖 5 min read (~ 1000 words).

String

Asserting Strings

Assertions

GoDoc

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

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

NotRegexp

NotRegexp asserts that a specified regular expression does not match a string.

See Regexp.

Examples
	assertions.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
	assertions.NotRegexp(t, "^start", "it's not starting")
	success: "^start", "not starting"
	failure: "^start", "starting"
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestNotRegexp(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 TestNotRegexp(t *testing.T)
    	require.NotRegexp(t, "^start", "not starting")
    	fmt.Println("passed")
    
    }

NotRegexpT[Rex RegExp, ADoc Text]

NotRegexpT asserts that a specified regular expression does not match a string.

See RegexpT.

Examples
	assertions.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
	assertions.NotRegexp(t, "^start", "it's not starting")
	success: "^start", "not starting"
	failure: "^start", "starting"
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestNotRegexpT(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 TestNotRegexpT(t *testing.T)
    	require.NotRegexpT(t, "^start", "not starting")
    	fmt.Println("passed")
    
    }

Regexp

Regexp asserts that a specified regular expression matches a string.

The regular expression may be passed as a regexp.Regexp, a string or a []byte and will be compiled.

The actual argument to be matched may be a string, []byte or anything that prints as a string with fmt.Sprint.

Examples
	assertions.Regexp(t, regexp.MustCompile("start"), "it's starting")
	assertions.Regexp(t, "start...$", "it's not starting")
	success: "^start", "starting"
	failure: "^start", "not starting"
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestRegexp(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 TestRegexp(t *testing.T)
    	require.Regexp(t, "^start", "starting")
    	fmt.Println("passed")
    
    }

RegexpT[Rex RegExp, ADoc Text]

RegexpT asserts that a specified regular expression matches a string.

The actual argument to be matched may be a string or []byte.

See Regexp.

Examples
  • Copy and click to open Go Playground

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

    // real-world test would inject *testing.T from TestRegexpT(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 TestRegexpT(t *testing.T)
    	require.RegexpT(t, "^start", "starting")
    	fmt.Println("passed")
    
    }


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