testing

package
v2.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustPanic

func MustPanic(testFunc func())

MustPanic 有錯誤才是正常,沒有錯誤會引發panic("should panic") If you know that you will get a panic, then you can call this function that will do recover.

Example

If you know that you will get a panic, then you can call this function that will do recover.

MustPanic(func() {
	panic("raise a panic")
})
fmt.Println("I can run in here")
Output:

I can run in here

func TestPanic

func TestPanic(testFunc func()) (reason any, isPanic bool)

TestPanic 在測試的時候,我們會預期有panic,但如果想要確定panic的原因是否真的如同我們猜測,就可以使用這個函數幫忙

Example
// example1
reason, isPanic := TestPanic(func() {
	panic("invalid memory address")
})
fmt.Printf("%v | %v\n", reason, isPanic)

// example2
reason, isPanic = TestPanic(func() {
	panic(errors.New("reason type: error"))
})
fmt.Printf("%v | %v\n", reason.(error).Error(), isPanic)

// example3
reason, isPanic = TestPanic(func() {
	_ = fmt.Sprintln("hello world")
})
fmt.Printf("%v | %v\n", reason, isPanic)
Output:

invalid memory address | true
reason type: error | true
<nil> | false

Types

This section is empty.

Jump to

Keyboard shortcuts

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