Documentation
¶
Overview ¶
Package got is an enjoyable golang test framework.
Index ¶
- func DefaultFlags(flags ...string)
- func Each(t Testable, iteratee interface{}) (count int)
- func EnsureCoverage(path string, min float64) error
- func Parallel() (n int)
- func Setup(init func(g G)) func(t Testable) G
- type AssertionCtx
- type AssertionErrType
- type AssertionError
- type AssertionErrorReport
- type Assertions
- func (as Assertions) Count(n int) func()
- func (as Assertions) Desc(format string, args ...interface{}) Assertions
- func (as Assertions) E(args ...interface{})
- func (as Assertions) Eq(x, y interface{})
- func (as Assertions) Equal(x, y interface{})
- func (as Assertions) Err(args ...interface{})
- func (as Assertions) False(x bool)
- func (as Assertions) Gt(x, y interface{})
- func (as Assertions) Gte(x, y interface{})
- func (as Assertions) Has(container, item interface{})
- func (as Assertions) InDelta(x, y interface{}, delta float64)
- func (as Assertions) Is(x, y interface{})
- func (as Assertions) Len(list interface{}, l int)
- func (as Assertions) Lt(x, y interface{})
- func (as Assertions) Lte(x, y interface{})
- func (as Assertions) Must() Assertions
- func (as Assertions) Neq(x, y interface{})
- func (as Assertions) Nil(args ...interface{})
- func (as Assertions) NotNil(args ...interface{})
- func (as Assertions) NotZero(x interface{})
- func (as Assertions) Panic(fn func()) (val interface{})
- func (as Assertions) Regex(pattern, str string)
- func (as Assertions) True(x bool)
- func (as Assertions) Zero(x interface{})
- type Context
- type G
- type Only
- type ReqMIME
- type ResHelper
- type Router
- type Skip
- type Testable
- type Utils
- func (ut Utils) Chdir(dir string)
- func (ut Utils) Context() Context
- func (ut Utils) DoAfter(d time.Duration, do func()) (cancel func())
- func (ut Utils) Error(args ...interface{})
- func (ut Utils) Errorf(format string, args ...interface{})
- func (ut Utils) Fatal(args ...interface{})
- func (ut Utils) Fatalf(format string, args ...interface{})
- func (ut Utils) Go(f func())
- func (ut Utils) HandleHTTP(file string, value ...interface{}) func(http.ResponseWriter, *http.Request)
- func (ut Utils) JSON(src interface{}) (v interface{})
- func (ut Utils) Log(args ...interface{})
- func (ut Utils) MkdirAll(perm fs.FileMode, path string)
- func (ut Utils) Open(create bool, path string) (f *os.File)
- func (ut Utils) PanicAfter(d time.Duration) (cancel func())
- func (ut Utils) Parallel() Utils
- func (ut Utils) PathExists(path string) bool
- func (ut Utils) RandBytes(l int) []byte
- func (ut Utils) RandInt(min, max int) int
- func (ut Utils) RandStr(l int) string
- func (ut Utils) Read(value interface{}) *bytes.Buffer
- func (ut Utils) Render(value interface{}, data interface{}) *bytes.Buffer
- func (ut Utils) Req(method, url string, options ...interface{}) *ResHelper
- func (ut Utils) Run(name string, f func(t G)) bool
- func (ut Utils) Serve() *Router
- func (ut Utils) ServeWith(network, address string) *Router
- func (ut Utils) Setenv(key, value string)
- func (ut Utils) Skip(args ...interface{})
- func (ut Utils) Skipf(format string, args ...interface{})
- func (ut Utils) Timeout(d time.Duration) Context
- func (ut Utils) ToJSON(obj interface{}) *bytes.Buffer
- func (ut Utils) ToJSONString(obj interface{}) string
- func (ut Utils) Write(obj interface{}) (writer func(io.Writer))
- func (ut Utils) WriteFile(path string, content interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultFlags ¶ added in v0.6.2
func DefaultFlags(flags ...string)
DefaultFlags will set the "go test" flag if not yet presented. It must be executed in the init() function. Such as the timeout:
DefaultFlags("timeout=10s")
func Each ¶ added in v0.0.3
Each runs each exported method Fn on type Ctx as a subtest of t. The iteratee can be a struct Ctx or:
iteratee(t Testable) (ctx Ctx)
Each Fn will be called like:
ctx.Fn()
If iteratee is Ctx, its G field will be set to New(t) for each test. Any Fn that has the same name with the embedded one will be ignored.
func EnsureCoverage ¶ added in v0.8.7
EnsureCoverage via report file generated from, for example:
go test -coverprofile=coverage.out
Return error if any file's coverage is less than min, min is a percentage value.
Types ¶
type AssertionCtx ¶ added in v0.21.0
type AssertionCtx struct { Type AssertionErrType Details []interface{} File string Line int }
AssertionCtx holds the context of an assertion
type AssertionErrType ¶ added in v0.21.0
type AssertionErrType int
AssertionErrType enum
const ( // AssertionEq type AssertionEq AssertionErrType = iota // AssertionNeqSame type AssertionNeqSame // AssertionNeq type AssertionNeq // AssertionGt type AssertionGt // AssertionGte type AssertionGte // AssertionLt type AssertionLt // AssertionLte type AssertionLte // AssertionInDelta type AssertionInDelta // AssertionTrue type AssertionTrue // AssertionFalse type AssertionFalse // AssertionNil type AssertionNil // AssertionNoArgs type AssertionNoArgs // AssertionNotNil type AssertionNotNil // AssertionNotNilable type AssertionNotNilable // AssertionNotNilableNil type AssertionNotNilableNil // AssertionZero type AssertionZero // AssertionNotZero type AssertionNotZero // AssertionRegex type AssertionRegex // AssertionHas type AssertionHas // AssertionLen type AssertionLen // AssertionErr type AssertionErr // AssertionPanic type AssertionPanic // AssertionIsInChain type AssertionIsInChain // AssertionIsKind type AssertionIsKind // AssertionCount type AssertionCount // AssertionSnapshot type AssertionSnapshot )
type AssertionError ¶ added in v0.21.0
type AssertionError interface {
Report(*AssertionCtx) string
}
AssertionError handler
func NewDefaultAssertionError ¶ added in v0.21.0
func NewDefaultAssertionError(theme gop.Theme, diffTheme diff.Theme) AssertionError
NewDefaultAssertionError handler
type AssertionErrorReport ¶ added in v0.21.0
type AssertionErrorReport func(*AssertionCtx) string
AssertionErrorReport is used to convert a func to AssertionError
func (AssertionErrorReport) Report ¶ added in v0.21.0
func (ae AssertionErrorReport) Report(ac *AssertionCtx) string
Report interface
type Assertions ¶ added in v0.4.1
type Assertions struct { Testable ErrorHandler AssertionError // contains filtered or unexported fields }
Assertions helpers
func (Assertions) Count ¶ added in v0.8.4
func (as Assertions) Count(n int) func()
Count asserts that the returned function will be called n times
func (Assertions) Desc ¶ added in v0.12.0
func (as Assertions) Desc(format string, args ...interface{}) Assertions
Desc returns a clone with the format description. The description will be printed before the error message.
func (Assertions) E ¶ added in v0.4.1
func (as Assertions) E(args ...interface{})
E is a shortcut for Must().Nil(args...)
func (Assertions) Eq ¶ added in v0.4.1
func (as Assertions) Eq(x, y interface{})
Eq asserts that x equals y when converted to the same type, such as compare float 1.0 and integer 1 . For strict value and type comparison use Assertions.Equal . For how comparison works, see utils.SmartCompare .
func (Assertions) Equal ¶ added in v0.4.1
func (as Assertions) Equal(x, y interface{})
Equal asserts that x equals y. For loose type comparison use Assertions.Eq, such as compare float 1.0 and integer 1 .
func (Assertions) Err ¶ added in v0.4.1
func (as Assertions) Err(args ...interface{})
Err asserts that the last item in args is error
func (Assertions) False ¶ added in v0.4.1
func (as Assertions) False(x bool)
False asserts that x is false.
func (Assertions) Gt ¶ added in v0.4.1
func (as Assertions) Gt(x, y interface{})
Gt asserts that x is greater than y. For how comparison works, see utils.SmartCompare .
func (Assertions) Gte ¶ added in v0.4.1
func (as Assertions) Gte(x, y interface{})
Gte asserts that x is greater than or equal to y. For how comparison works, see utils.SmartCompare .
func (Assertions) Has ¶ added in v0.4.1
func (as Assertions) Has(container, item interface{})
Has asserts that container has item. The container can be a string, []byte, slice, array, or map. For how comparison works, see utils.SmartCompare .
func (Assertions) InDelta ¶ added in v0.16.1
func (as Assertions) InDelta(x, y interface{}, delta float64)
InDelta asserts that x and y are within the delta of each other. For how comparison works, see utils.SmartCompare .
func (Assertions) Is ¶ added in v0.4.1
func (as Assertions) Is(x, y interface{})
Is asserts that x is kind of y, it uses reflect.Kind to compare. If x and y are both error type, it will use errors.Is to compare.
func (Assertions) Len ¶ added in v0.4.1
func (as Assertions) Len(list interface{}, l int)
Len asserts that the length of list equals l
func (Assertions) Lt ¶ added in v0.4.1
func (as Assertions) Lt(x, y interface{})
Lt asserts that x is less than y. For how comparison works, see utils.SmartCompare .
func (Assertions) Lte ¶ added in v0.4.1
func (as Assertions) Lte(x, y interface{})
Lte asserts that x is less than or equal to b. For how comparison works, see utils.SmartCompare .
func (Assertions) Must ¶ added in v0.12.0
func (as Assertions) Must() Assertions
Must returns a clone with the FailNow enabled. It will exit the current goroutine if the assertion fails.
func (Assertions) Neq ¶ added in v0.4.1
func (as Assertions) Neq(x, y interface{})
Neq asserts that x not equals y even when converted to the same type. For how comparison works, see utils.SmartCompare .
func (Assertions) Nil ¶ added in v0.4.1
func (as Assertions) Nil(args ...interface{})
Nil asserts that the last item in args is nilable and nil
func (Assertions) NotNil ¶ added in v0.4.1
func (as Assertions) NotNil(args ...interface{})
NotNil asserts that the last item in args is nilable and not nil
func (Assertions) NotZero ¶ added in v0.15.0
func (as Assertions) NotZero(x interface{})
NotZero asserts that x is not zero value for its type.
func (Assertions) Panic ¶ added in v0.4.1
func (as Assertions) Panic(fn func()) (val interface{})
Panic executes fn and asserts that fn panics
func (Assertions) Regex ¶ added in v0.4.1
func (as Assertions) Regex(pattern, str string)
Regex asserts that str matches the regex pattern
func (Assertions) True ¶ added in v0.4.1
func (as Assertions) True(x bool)
True asserts that x is true.
func (Assertions) Zero ¶ added in v0.15.0
func (as Assertions) Zero(x interface{})
Zero asserts x is zero value for its type.
type G ¶ added in v0.2.0
type G struct { Testable Assertions Utils // contains filtered or unexported fields }
G is the helper context, it provides some handy helpers for testing
func (G) Snapshot ¶ added in v0.36.2
Snapshot asserts that x equals the snapshot with the specified name, name should be unique under the same test case. It will create a new snapshot file if the name is not found. The snapshot file will be saved to ".got/snapshots/{TEST_NAME}". To update the snapshot, just change the name of the snapshot or remove the corresponding snapshot file. It will auto-remove the unused snapshot files after the test. The snapshot files should be version controlled. The format of the snapshot file is json.
type ReqMIME ¶ added in v0.11.0
type ReqMIME string
ReqMIME option type, it should be like ".json", "test.json", "a/b/c.jpg", etc
type ResHelper ¶ added in v0.2.0
ResHelper of the request
func (*ResHelper) Bytes ¶ added in v0.2.0
Bytes parses body as *bytes.Buffer and returns the result
func (*ResHelper) JSON ¶ added in v0.2.0
func (res *ResHelper) JSON() (v interface{})
JSON parses body as json and returns the result
func (*ResHelper) Unmarshal ¶ added in v0.38.2
func (res *ResHelper) Unmarshal(v interface{})
Unmarshal body to v as json, it's like json.Unmarshal.
type Router ¶ added in v0.2.0
type Router struct { HostURL *url.URL Server *http.Server Mux *http.ServeMux // contains filtered or unexported fields }
Router of a http server
func (*Router) Route ¶ added in v0.2.0
Route on the pattern. Check the doc of http.ServeMux for the syntax of pattern. It will use Utils.HandleHTTP to handle each request.
type Testable ¶ added in v0.0.3
type Testable interface { Name() string // same as testing.common.Name Skipped() bool // same as testing.common.Skipped Failed() bool // same as testing.common.Failed Cleanup(func()) // same as testing.common.Cleanup FailNow() // same as testing.common.FailNow Fail() // same as testing.common.Fail Helper() // same as testing.common.Helper Logf(format string, args ...interface{}) // same as testing.common.Logf SkipNow() // same as testing.common.Skip }
Testable interface. Usually, you use *testing.T as it.
type Utils ¶ added in v0.4.2
type Utils struct {
Testable
}
Utils for commonly used methods
func (Utils) Error ¶ added in v0.4.2
func (ut Utils) Error(args ...interface{})
Error is the same as [testing.common.Error]
func (Utils) Fatal ¶ added in v0.4.2
func (ut Utils) Fatal(args ...interface{})
Fatal is the same as [testing.common.Fatal]
func (Utils) Go ¶ added in v0.38.3
func (ut Utils) Go(f func())
Go runs f in a goroutine and wait for it to finish before the test ends.
func (Utils) HandleHTTP ¶ added in v0.4.2
func (ut Utils) HandleHTTP(file string, value ...interface{}) func(http.ResponseWriter, *http.Request)
HandleHTTP handles a request. If file exists serve the file content. The file will be used to set the Content-Type header. If the file doesn't exist, the value will be encoded by Utils.Write and used as the response body.
func (Utils) JSON ¶ added in v0.4.2
func (ut Utils) JSON(src interface{}) (v interface{})
JSON from string, []byte, or io.Reader
func (Utils) Log ¶ added in v0.4.2
func (ut Utils) Log(args ...interface{})
Log is the same as [testing.common.Log]
func (Utils) MkdirAll ¶ added in v0.32.0
MkdirAll is like os.MkdirAll but will remove the dir after test and fail the test if error. The default perm is 0755.
func (Utils) Open ¶ added in v0.4.2
Open a file. Override it if create is true. Directories will be auto-created. If the directory and file doesn't exist, it will be removed after the test.
func (Utils) PanicAfter ¶ added in v0.7.0
PanicAfter d duration if the test is still running
func (Utils) Parallel ¶ added in v0.4.2
Parallel is the same as testing.T.Parallel
func (Utils) PathExists ¶ added in v0.32.0
PathExists checks if path exists
func (Utils) RandBytes ¶ added in v0.34.1
RandBytes generates a random byte array with the specified length
func (Utils) Read ¶ added in v0.4.2
Read all from value. If the value is string and it's a file path, the file content will be read, or the string will be returned. If the value is io.Reader, the reader will be read. If the value is []byte, the value will be returned. Others will be converted to string and returned.
func (Utils) Render ¶ added in v0.32.0
Render template. It will use Utils.Read to read the value as the template string.
func (Utils) Req ¶ added in v0.4.2
Req is a helper method to send http request. It will handle errors automatically, so you don't need to check errors. The method is the http method, default value is "GET". If an option is http.Header, it will be used as the request header. If an option is ReqMIME, it will be used to set the Content-Type header. If an option is context.Context, it will be used as the request context. Other option type will be treat as request body, it will be encoded by Utils.Write. Some request examples:
Req("GET", "http://example.com") Req("GET", "http://example.com", context.TODO()) Req("POST", "http://example.com", map[string]any{"a": 1}) Req("POST", "http://example.com", http.Header{"Host": "example.com"}, ReqMIME(".json"), map[string]any{"a": 1})
func (Utils) Serve ¶ added in v0.4.2
Serve http on a random port. The server will be auto-closed after the test.
func (Utils) Setenv ¶ added in v0.34.0
Setenv is like os.Setenv but will restore the env after test.
func (Utils) Skip ¶ added in v0.4.2
func (ut Utils) Skip(args ...interface{})
Skip is the same as [testing.common.Skip]
func (Utils) ToJSONString ¶ added in v0.10.0
ToJSONString convert obj to JSON string
func (Utils) Write ¶ added in v0.4.2
Write obj to the writer. Encode obj to []byte and cache it for writer. If obj is not []byte, string, or io.Reader, it will be encoded as JSON.
func (Utils) WriteFile ¶ added in v0.31.2
WriteFile at path with content, it uses Utils.Open to open the file.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
check-cov
Package main ...
|
Package main ... |
fixtures
|
|
coverage
Package coverage ...
|
Package coverage ... |
lib
|
|
diff
Package diff ...
|
Package diff ... |
got-vscode-extension/snippets
Package main ...
|
Package main ... |
lcs
Package lcs ...
|
Package lcs ... |
mock
Package mock provides a simple way to stub struct methods.
|
Package mock provides a simple way to stub struct methods. |
utils
Package utils ...
|
Package utils ... |
benchmark
Module
|
|
example
Module
|