is

package module
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2023 License: MIT Imports: 9 Imported by: 0

README

Is Go Go Report Card codecov Go Reference

A lightweight testing framework for golang.

Usage

Basic usage
func TestLoader(t *testing.T){
    is := is.New(t)
    
    l := loader{url: "http://example.com"}
    
    r, err := l.Get()
    is(l.url == "http://example.com", "calling Get() should not modify url")
    if err == nil{
        is(r != nil, "response should not be nil if err != nil")
        is.Equal(r, testData, "the page content must match")
    } else {
         is.Log("Failed to get test data. Skipping test.")
    }

}
}

Test Suites
type LoaderTest struct{
    data []byte
    loader *loader
}

func (l *LoaderTest) Setup(){
    l.loader = &loader{}
}

func (l *LoaderTest) TestUrl(is is.Is){
    // tests go here
}

func (l *LoaderTest) Teardown(){
    l.loader.Close()
}

func TestLoader(t *testing.T){
    is.Suite(t, &LoaderTest{})
}

Functions

  • Is.Equal - Fails if the provided values are not are deeply equal
  • Is.Panic - Fails if recover() returns nil
  • Is.Fail - Fails the test with the given message

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Suite

func Suite(t *testing.T, suite interface{}, opts ...Option)

Suite runs the given test suite. All test functions must have the prefixed with Test and must take Is as the first and only argument and should not have a return value. Test functions are run sequentially in lexicographic order. To run tests in parallel use SuiteP.

// Example test
func (s *SuiteName) TestName(Is){}

A test suite can define a Setup and Teardown function to setup the test suite and cleanup after the suite has completed. Both setup and teardown functions must have no arguments and return values. The Teardown function is always called after running all tests even if the tests fail.

func (s *suiteName) Setup(){ /* setup the set suite here */ }
func (s *suiteName) Teardown(){ /* clean up after the test suite has completed. */ }

func SuiteP

func SuiteP(t *testing.T, suite interface{}, opts ...Option)

SuiteP like Suite but calls all test function in parallel.

Types

type Is

type Is func(cond bool, msg string, i ...interface{})

Is is provides helpers for writing tests.

func New

func New(t *testing.T, opts ...Option) Is

New creates a new test

func (Is) Equal

func (is Is) Equal(value, expected interface{}, format string, i ...interface{})

Equal checks if the given values are equal.

func (Is) Err added in v2.2.2

func (is Is) Err(err, target error, format string, args ...interface{})

Err checks if any error in err's chain matches target. If no errors match target, the test fails.

func (Is) Fail

func (is Is) Fail(format string, args ...interface{})

Fail immediately fails the test. Calling this function is the equivalent of calling is.T().Fatalf.

func (Is) Log

func (is Is) Log(msg string, i ...interface{})

Log logs the given message. This is the equivalent of calling is.T().Log(msg). This function can be called from multiple goroutines concurrently.

func (Is) Panic

func (is Is) Panic(fn func(), format string, i ...interface{})

Panic checks if calling the given function causes a panic. If the given function does not panic the test fails.

func (Is) Run

func (is Is) Run(name string, testFn func(Is))

Run runs the given sub test. This runs testFn in a separate goroutine and blocks until f returns or calls is.T().Parallel to become a parallel test.

func (Is) RunP

func (is Is) RunP(name string, testFn func(Is))

RunP runs the given test in parallel with the current test.

func (Is) T

func (is Is) T() *testing.T

T gets the underlying *testing.T for this test.

type Option added in v2.4.0

type Option func(*options)

Option an options to be applied to the test.

func CmpAllUnexported added in v2.4.0

func CmpAllUnexported() Option

CmpAllUnexported enables comparing all unexported fields using Is.Equal. Using this options is not recommended since this will compare the unexported fields of structs from other packages. Use CmpUnexported instead.

func CmpOpt added in v2.4.0

func CmpOpt(opts ...cmp.Option) Option

CmpOpt adds the given [cmp.Option]s to the list of options passed to cmp.Diff.

func CmpUnexported added in v2.4.0

func CmpUnexported(types ...interface{}) Option

CmpUnexported allows comparing unexported fields of the given struct types.

func EquateEmpty added in v2.4.0

func EquateEmpty(eq bool) Option

EquateEmpty sets if empty maps/slices and nil maps/slices are considered to be equal. Default: true

func EquateErrors added in v2.4.0

func EquateErrors(eq bool) Option

EquateErrors sets if the equality of errors is checked using errors.Is. Default: false

func EquateNaN added in v2.4.0

func EquateNaN(eq bool) Option

EquateNaN sets if two NaN values are considered equal Default: true

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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