testarossa

package module
v0.7.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2025 License: Apache-2.0 Imports: 10 Imported by: 1

README

Testa Rossa

TestaRossa is a simple utility on top of Go's standard testing library that prints the full/path/to/the/source/file/of/the/unit_test.go:line of a failed assertion, which in VSCode is a clickable link. Unlike t.Log, output is printed using fmt.Printf during the execution of the test rather than at its conclusion.

TestaRossa supports a functional pattern:

func TestMe(t *testing.T) {
    neo := 1999
    testarossa.Equal(t, 1, neo, "You are not the %d", 1)
    
    err := errors.New("This is bad")
    testarossa.NoError(t, err)

    html := `<html><body><div class="banner">Hello, <b>World</b>!</div></body></html>`
    testarossa.HTMLMatch(t, html, "DIV.banner", "^World$")

    droids := 1234
    testarossa.FailIf(t, droids != 0, "These are not the droids you are looking for")
    
    err = errors.New("This is really bad")
    testarossa.FatalIfError(t, err)
}

as well as a more object-oriented pattern:

func TestMe(t *testing.T) {
    assert := testarossa.For(t)

    neo := 1999
    assert.Equal(1, neo, "You are not the %d", 1)
    
    err := errors.New("This is bad")
    assert.NoError(err)
    
    result, err := doSomething(param)
    assert.Expect(err, nil, result, 1234)

    html := `<html><body><div class="banner">Hello, <b>World</b>!</div></body></html>`
    assert.HTMLMatch(html, "DIV.banner", "^World$")
}

Example test output:

--- FAIL: TestMe
    /my_projects/github.com/microbus-io/testarossa/my_test.go:10
    Expected '1', actual '1999'
    You are not the 1
--- FAIL: TestMe
    /my_projects/github.com/microbus-io/testarossa/my_test.go:12
    Expected no error
    This is bad
--- FAIL: TestMe (0.00s)
FAIL
FAIL	github.com/microbus-io/testarossa	0.173s
FAIL

TestaRossa is licensed by Microbus LLC under the Apache License 2.0.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(t TestingT, whole any, sub any, args ...any) bool

Contains fails the test if a string or error don't contain a substring, or if a byte slice doesn't contain a byte subslice, or if a slice doesn't contain an element, or if a map doesn't contain a key.

func Equal

func Equal(t TestingT, expected any, actual any, args ...any) bool

Equal fails the test if the two values are not equal. Note: the expected value comes before the actual value in the argument list.

func Error

func Error(t TestingT, err error, args ...any) bool

Error fails the test if err is nil.

func ErrorContains deprecated

func ErrorContains(t TestingT, err error, substr string, args ...any) bool

ErrorContains fails the test if the err is nil or if it does not contain the substring.

Deprecated: Use Contains

func Expect added in v0.6.0

func Expect(t TestingT, actualExpectedPairs ...any) bool

Expect fails the test if any of the paired values are not equal. Note: the expected value comes after the actual value in each pair.

result, err := doSomething(p1, p2)
Expect(t, result, 4321, err, nil)

func FailIf

func FailIf(t TestingT, condition bool, args ...any) bool

FailIf fails the test if the condition is met. If returns back the result of evaluating the condition.

func FailIfError

func FailIfError(t TestingT, err error, args ...any) bool

FailIfError is a shortcut to FailIf(t, err != nil, append([]any{err}, args...)...) .

func False

func False(t TestingT, condition bool, args ...any) bool

False fails the test if the condition is true.

func FatalIf

func FatalIf(t TestingT, condition bool, args ...any) bool

FatalIf fails the test and stops further execution if the condition is met.

func FatalIfError

func FatalIfError(t TestingT, err error, args ...any) bool

FatalIfError is a shortcut to FatalIf(t, err != nil, append([]any{err}, args...)...) .

func HTMLMatch added in v0.7.0

func HTMLMatch(t TestingT, htmlBody []byte, cssSelectorQuery string, innerTextRegExp string, args ...any) bool

HTMLMatch fails the test if no HTML element matching the CSS selector query was found to also match the regular expression against the inner text of any of its descendants.

Examples:

HTMLMatch(t, html, `TR > TD > A.expandable[href]`, "")
HTMLMatch(t, html, `DIV#main_panel`, `^Help$``)
HTMLMatch(t, html, `TR TD INPUT[name="x"]`, `[0-9]+``)

func HTMLNotMatch added in v0.7.0

func HTMLNotMatch(t TestingT, htmlBody []byte, cssSelectorQuery string, innerTextRegExp string, args ...any) bool

HTMLNotMatch fails the test if at least one HTML element matching the CSS selector query was found to also match the regular expression against the inner text of one of its descendants.

Examples:

HTMLNotMatch(t, html, `TR > TD > A.expandable[href]`, "")
HTMLNotMatch(t, html, `DIV#main_panel`, "^Help$")
HTMLNotMatch(t, html, `TR TD INPUT[name="x"]`, `[0-9]+``)

func Len added in v0.3.3

func Len(t TestingT, obj any, length int, args ...any) bool

Len fails the test if the length of the string, slice, array, map or chan does not match the expected len.

func MapLen deprecated added in v0.3.3

func MapLen[K comparable, V any](t TestingT, m map[K]V, length int, args ...any) bool

MapLen fails the test if the length of the map does not match the expected len.

Deprecated: Use Len

func Match added in v0.7.2

func Match(t TestingT, whole string, regexpStr string, args ...any) bool

Match fails the test if a string doesn't match a regular expression.

func Nil

func Nil(t TestingT, obj any, args ...any) bool

Nil fails the test if the object is not nil.

func NoError

func NoError(t TestingT, err error, args ...any) bool

NoError fails the test if err is not nil.

func NotContains

func NotContains(t TestingT, whole any, sub any, args ...any) bool

NotContains fails the test if a string or error contain a substring, or if a byte slice contains a byte subslice, or if a slice contains an element, or if a map contains a key.

func NotEqual

func NotEqual(t TestingT, unexpected any, actual any, args ...any) bool

NotEqual fails the test if the two values are equal. Note: the expected value comes before the actual value in the argument list.

func NotMatch added in v0.7.2

func NotMatch(t TestingT, whole string, regexpStr string, args ...any) bool

NotMatch fails the test if a string matches a regular expression.

func NotNil

func NotNil(t TestingT, obj any, args ...any) bool

NotNil fails the test if the object is nil.

func NotZero

func NotZero(t TestingT, actual any, args ...any) bool

NotZero fails the test if the value is the 0 value of its type. Nils are considered zero.

func SliceContains deprecated

func SliceContains[T comparable](t TestingT, slice []T, contains T, args ...any) bool

SliceContains fails the test if the slice does not contain the item.

Deprecated: Use Contains

func SliceEqual deprecated

func SliceEqual[T comparable](t TestingT, expected []T, actual []T, args ...any) bool

SliceEqual fails the test if the two values are not equal.

Deprecated: Use Equal

func SliceLen deprecated

func SliceLen[T any](t TestingT, slice []T, length int, args ...any) bool

SliceLen fails the test if the length of the slice does not match the expected len.

Deprecated: Use Len

func SliceNotContains deprecated

func SliceNotContains[T comparable](t TestingT, slice []T, contains T, args ...any) bool

SliceNotContains fails the test if the slice contains the item.

Deprecated: Use NotContains

func SliceNotEqual deprecated

func SliceNotEqual[T comparable](t TestingT, expected []T, actual []T, args ...any) bool

SliceNotEqual fails the test if the two values are equal.

Deprecated: Use NotEqual

func StrLen deprecated

func StrLen(t TestingT, s string, length int, args ...any) bool

StrLen fails the test if the length of the string does not match the expected len.

Deprecated: Use Len

func True

func True(t TestingT, condition bool, args ...any) bool

True fails the test if the condition is false.

func Zero

func Zero(t TestingT, actual any, args ...any) bool

Zero fails the test if the value is not the 0 value of its type. Nils are considered zero.

Types

type Asserter added in v0.4.0

type Asserter struct {
	// contains filtered or unexported fields
}

func For added in v0.4.0

func For(t TestingT) *Asserter

func (*Asserter) Contains added in v0.4.0

func (tt *Asserter) Contains(whole any, sub any, args ...any) bool

Contains fails the test if the string does not contain a substring.

func (*Asserter) Equal added in v0.4.0

func (tt *Asserter) Equal(expected any, actual any, args ...any) bool

Equal fails the test if the two values are not equal. Note: the expected value comes before the actual value in the argument list.

func (*Asserter) Error added in v0.4.0

func (tt *Asserter) Error(err error, args ...any) bool

Error fails the test if err is nil.

func (*Asserter) Expect added in v0.6.0

func (tt *Asserter) Expect(actualExpectedPairs ...any) bool

Expect fails the test if any of the paired values are not equal. Note: the expected value comes after the actual value in each pair.

result, err := doSomething(p1, p2)
tt.Expect(err, nil, result, 4321)

func (*Asserter) False added in v0.4.0

func (tt *Asserter) False(condition bool, args ...any) bool

False fails the test if the condition is true.

func (*Asserter) HTMLMatch added in v0.7.0

func (tt *Asserter) HTMLMatch(htmlBody []byte, cssSelectorQuery string, innerTextRegExp string, args ...any) bool

HTMLMatch fails the test if no HTML element matching the CSS selector query was found to also match the regular expression by the inner text of any of its descendants.

Examples:

tt.HTMLMatch(html, `TR > TD > A.expandable[href]`, "")
tt.HTMLMatch(html, `DIV#main_panel`, `^Help$``)
tt.HTMLMatch(html, `TR TD INPUT[name="x"]`, `[0-9]+``)

func (*Asserter) HTMLNotMatch added in v0.7.0

func (tt *Asserter) HTMLNotMatch(htmlBody []byte, cssSelectorQuery string, innerTextRegExp string, args ...any) bool

HTMLNotMatch fails the test if at least one HTML element matching the CSS selector query was found to also match the regular expression by the inner text of any of its descendants.

Examples:

HTMLNotMatch(t, html, `TR > TD > A.expandable[href]`, "")
HTMLNotMatch(t, html, `DIV#main_panel`, "^Help$")
HTMLNotMatch(t, html, `TR TD INPUT[name="x"]`, `[0-9]+``)

func (*Asserter) Len added in v0.4.0

func (tt *Asserter) Len(obj any, length int, args ...any) bool

Len fails the test if the length of the string, slice, array, map or chan does not match the expected len.

func (*Asserter) Match added in v0.7.2

func (tt *Asserter) Match(whole string, regexpStr string, args ...any) bool

Match fails the test if a string doesn't match a regular expression.

func (*Asserter) Nil added in v0.4.0

func (tt *Asserter) Nil(obj any, args ...any) bool

Nil fails the test if the object is not nil.

func (*Asserter) NoError added in v0.4.0

func (tt *Asserter) NoError(err error, args ...any) bool

NoError fails the test if err is not nil.

func (*Asserter) NotContains added in v0.4.0

func (tt *Asserter) NotContains(whole any, sub any, args ...any) bool

NotContains fails the test if the string contain a substring.

func (*Asserter) NotEqual added in v0.4.0

func (tt *Asserter) NotEqual(expected any, actual any, args ...any) bool

NotEqual fails the test if the two values are equal. Note: the expected value comes before the actual value in the argument list.

func (*Asserter) NotMatch added in v0.7.2

func (tt *Asserter) NotMatch(whole string, regexpStr string, args ...any) bool

NotMatch fails the test if a string matches a regular expression.

func (*Asserter) NotNil added in v0.4.0

func (tt *Asserter) NotNil(obj any, args ...any) bool

NotNil fails the test if the object is nil.

func (*Asserter) NotZero added in v0.4.0

func (tt *Asserter) NotZero(actual any, args ...any) bool

NotZero fails the test if the value is the 0 value of its type.

func (*Asserter) True added in v0.4.0

func (tt *Asserter) True(condition bool, args ...any) bool

True fails the test if the condition is false.

func (*Asserter) Zero added in v0.4.0

func (tt *Asserter) Zero(actual any, args ...any) bool

Zero fails the test if the value is not 0.

type TestingT

type TestingT interface {
	Fail()
	FailNow()
	Name() string
}

TestingT is an interface that both *testing.T and *testing.B implement.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL