testutil

package
v0.5.11 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2022 License: MIT Imports: 9 Imported by: 1

README

Test Utils

  • assert go tests helper
  • env variable mocks
  • http request mock

Install

go get github.com/gookit/goutil/sysutil

assert tests

package assert_test

import (
	"testing"

	"github.com/gookit/goutil/errorx"
	"github.com/gookit/goutil/testutil/assert"
)

func TestErr(t *testing.T) {
	err := errorx.Raw("this is a error")

	assert.NoErr(t, err, "user custom message")
	assert.ErrMsg(t, err, "this is a error")
}

Run tests for special method:

go test -v -run ^TestErr$
go test -v -run ^TestErr$ ./testutil/assert/...

Error on fail:

test-err

Mock for tests

More test utils

Wraps buffer

testutil.Buffer is wraps the bytes.Buffer and useful for testing. Will not return error on call WriteX methods

func (b *Buffer) WriteString(ss ...string) // will not return error
func (b *Buffer) WriteAny(vs ...interface{})
func (b *Buffer) ResetAndGet() string
Wraps writer

testutil.TestWriter is wraps the testutil.Buffer. it can be custom return error on write, close, flush.

Examples:

package testutil_test

import (
	"testing"

	"github.com/gookit/goutil/testutil"
	"github.com/gookit/goutil/testutil/assert"
)

func TestNewTestWriter(t *testing.T) {
	tw := testutil.NewTestWriter()
	_, err := tw.Write([]byte("hello"))
	assert.NoErr(t, err)
	assert.Eq(t, "hello", tw.String())
	assert.NoErr(t, tw.Flush())
	assert.Eq(t, "", tw.String())
	assert.NoErr(t, tw.Close())

	tw.SetErrOnWrite()
	_, err = tw.Write([]byte("hello"))
	assert.Err(t, err)
	assert.Eq(t, "", tw.String())

	tw.SetErrOnFlush()
	assert.Err(t, tw.Flush())

	tw.SetErrOnClose()
	assert.Err(t, tw.Close())
}

Code Check & Testing

gofmt -w -l ./
golint ./...
go test ./...

Testing in docker:

cd goutil
docker run -ti -v $(pwd):/go/work golang:1.18
root@xx:/go/work# go test ./...

Upgrade interface{} to any:

# 查看此次替换会影响到的源文件列表
gofmt -l -r 'interface{} -> any' .
# do 
gofmt -w -r 'interface{} -> any' .

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiscardStdout

func DiscardStdout() error

DiscardStdout Discard os.Stdout output

Usage:

DiscardStdout()
fmt.Println("Hello, playground")
RestoreStdout()

func MockCleanOsEnv added in v0.5.5

func MockCleanOsEnv(mp map[string]string, fn func())

MockCleanOsEnv by env map data.

will clear all old ENV data, use given data map. will recover old ENV after fn run.

func MockEnvValue added in v0.1.6

func MockEnvValue(key, val string, fn func(nv string))

MockEnvValue will store old env value, set new val. will restore old value on end.

func MockEnvValues added in v0.1.6

func MockEnvValues(kvMap map[string]string, fn func())

MockEnvValues will store old env value, set new val. will restore old value on end.

func MockOsEnv added in v0.3.13

func MockOsEnv(mp map[string]string, fn func())

MockOsEnv by env map data. alias of MockCleanOsEnv

func MockOsEnvByText added in v0.3.13

func MockOsEnvByText(envText string, fn func())

MockOsEnvByText by env text string. will clear all old ENV data, use given data map. will recover old ENV after fn run.

func MockRequest

func MockRequest(h http.Handler, method, path string, data *MD) *httptest.ResponseRecorder

MockRequest mock an HTTP Request

Usage:

handler := router.New()
res := MockRequest(handler, "GET", "/path", nil)

// with data 1
body := strings.NewReader("string ...")
res := MockRequest(handler, "POST", "/path", &MD{
	Body: body,
	Headers: M{"x-head": "val"}
})

// with data 2
res := MockRequest(handler, "POST", "/path", &MD{
	BodyString: "data string",
	Headers: M{"x-head": "val"}
})

func NewHttpRequest added in v0.3.6

func NewHttpRequest(method, path string, data *MD) *http.Request

NewHttpRequest for http testing Usage:

req := NewHttpRequest("GET", "/path", nil)

// with data 1
body := strings.NewReader("string ...")
req := NewHttpRequest("POST", "/path", &MD{
	Body: body,
	Headers: M{"x-head": "val"}
})

// with data 2
req := NewHttpRequest("POST", "/path", &MD{
	BodyString: "data string",
	Headers: M{"x-head": "val"}
})

func RestoreStderr added in v0.2.10

func RestoreStderr(printData ...bool) (s string)

RestoreStderr restore os.Stderr

func RestoreStdout

func RestoreStdout(printData ...bool) (s string)

RestoreStdout restore os.Stdout

func RewriteStderr added in v0.2.10

func RewriteStderr()

RewriteStderr rewrite os.Stderr

Usage:

RewriteStderr()
fmt.Fprintln(os.Stderr, "Hello, playground")
msg := RestoreStderr()

func RewriteStdout

func RewriteStdout()

RewriteStdout rewrite os.Stdout

Usage:

RewriteStdout()
fmt.Println("Hello, playground")
msg := RestoreStdout()

Types

type Buffer added in v0.5.6

type Buffer struct {
	bytes.Buffer
}

Buffer wrap and extends the bytes.Buffer

func NewBuffer added in v0.5.6

func NewBuffer() *Buffer

NewBuffer instance

func (*Buffer) ResetAndGet added in v0.5.6

func (b *Buffer) ResetAndGet() string

ResetAndGet buffer string.

func (*Buffer) WriteAny added in v0.5.6

func (b *Buffer) WriteAny(vs ...interface{})

WriteAny method

func (*Buffer) WriteString added in v0.5.6

func (b *Buffer) WriteString(ss ...string)

WriteString rewrite

func (*Buffer) Writeln added in v0.5.6

func (b *Buffer) Writeln(s string)

Writeln method

type M

type M map[string]string

M short name for map

type MD

type MD struct {
	// Headers headers
	Headers M
	// Body body. eg: strings.NewReader("name=inhere")
	Body io.Reader
	// BodyString quick add body.
	BodyString string
	// BeforeSend callback
	BeforeSend func(req *http.Request)
}

MD simple request data

type TestWriter added in v0.5.3

type TestWriter struct {
	Buffer
	// ErrOnWrite return error on write, useful for testing
	ErrOnWrite bool
	// ErrOnFlush return error on flush, useful for testing
	ErrOnFlush bool
	// ErrOnClose return error on close, useful for testing
	ErrOnClose bool
}

TestWriter struct, useful for testing

func NewTestWriter added in v0.5.3

func NewTestWriter() *TestWriter

NewTestWriter instance

func (*TestWriter) Close added in v0.5.3

func (w *TestWriter) Close() error

Close implements

func (*TestWriter) Flush added in v0.5.3

func (w *TestWriter) Flush() error

Flush implements

func (*TestWriter) SetErrOnClose added in v0.5.6

func (w *TestWriter) SetErrOnClose()

SetErrOnClose method

func (*TestWriter) SetErrOnFlush added in v0.5.6

func (w *TestWriter) SetErrOnFlush()

SetErrOnFlush method

func (*TestWriter) SetErrOnWrite added in v0.5.6

func (w *TestWriter) SetErrOnWrite()

SetErrOnWrite method

func (*TestWriter) Write added in v0.5.3

func (w *TestWriter) Write(p []byte) (n int, err error)

Write implements

Directories

Path Synopsis
Package assert provides some tool functions for use with the Go testing.
Package assert provides some tool functions for use with the Go testing.

Jump to

Keyboard shortcuts

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