Test 单元测试
testing包提供了对Go包的自动测试支持。 这是和go test 命令相呼应的功能, go test 命令会自动执行所以符合格式
func TestXXX(t *testing.T)
的函数。
被测试函数
package mytest01
import (
"fmt"
"reflect"
)
type DelayFunc struct {
f func(v ...interface{})
args [] interface{}
}
func NewDelayFunc(f func(v...interface{}),args []interface{}) *DelayFunc {
return &DelayFunc{
f:f,
args:args,
}
}
func (this*DelayFunc)Call(){
defer func() {
if err:=recover();err!=nil {
fmt.Errorf("call err:",err)
}
}()
this.f(this.args...)
}
func (this*DelayFunc)String()string {
return fmt.Sprintf("delay function type %s ,args:%v",reflect.TypeOf(this.f),this.args)
}
测试函数
package mytest01
import (
"testing"
"fmt"
)
func TestDelay(t*testing.T) {
NewDelayFunc(foo,[]interface{}{1,2,2,3,4})
}
func foo(v ...interface{}) {
fmt.Println("the number of foo is ",v)
}
Benchmark 性能测试
Functions of the form
func BenchmarkXxx(b testing.B)
符合格式 (见上)的函数被认为是一个性能测试程序, 当带着 -bench=“.” ( 参数必须有!)来执行**go test命令的时候性能测试程序就会被顺序执行。
$ ls
CONTRIBUTING.md dependencies.go goconvey LICENSE.md web
convey examples goconvey.go README.md
$ ./goconvey
2018/12/21 16:57:33 goconvey.go:63: Initial configuration: [host: 127.0.0.1] [port: 8080] [poll: 250ms] [cover: true]
2018/12/21 16:57:33 tester.go:19: Now configured to test 10 packages concurrently.
2018/12/21 16:57:33 goconvey.go:194: Serving HTTP at: http://127.0.0.1:8080
2018/12/21 16:57:33 goconvey.go:107: Launching browser on 127.0.0.1:8080
2018/12/21 16:57:33 integration.go:122: File system state modified, publishing current folders... 0 115903539471
2018/12/21 16:57:33 goconvey.go:120: Received request from watcher to execute tests...
2018/12/21 16:57:33 executor.go:69: Executor status: 'executing'
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey
2018/12/21 16:57:33 coordinator.go:37: Skipping concurrent execution: github.com/smartystreets/goconvey/web/server/watch/integration_testing/sub
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/examples
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/fonts/FontAwesome/css
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/css/themes
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/fonts/Open_Sans
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/server/system
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/js
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/server/contract
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/server/messaging
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/reports
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/fonts/Oswald
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/js/lib
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/convey
2018/12/21 16:57:33 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/convey/gotest
2018/12/21 16:57:34 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/server/executor
2018/12/21 16:57:34 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/server/watch/integration_testing
2018/12/21 16:57:34 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources
2018/12/21 16:57:35 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/fonts/Orbitron
2018/12/21 16:57:35 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web
2018/12/21 16:57:35 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client
2018/12/21 16:57:35 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/server/parser
2018/12/21 16:57:36 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/convey/reporting
2018/12/21 16:57:36 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/fonts/FontAwesome/fonts
2018/12/21 16:57:36 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/ico
2018/12/21 16:57:36 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/server
2018/12/21 16:57:36 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/server/api
2018/12/21 16:57:36 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/server/watch
2018/12/21 16:57:37 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/css
2018/12/21 16:57:37 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/fonts
2018/12/21 16:57:37 coordinator.go:46: Executing concurrent tests: github.com/smartystreets/goconvey/web/client/resources/fonts/FontAwesome
2018/12/21 16:57:37 shell.go:102: Coverage output: ? github.com/smartystreets/goconvey [no test files]
2018/12/21 16:57:37 shell.go:104: Run without coverage
2018/12/21 16:57:37 goconvey.go:115: Created new window in existing browser session.
2018/12/21 16:57:39 parser.go:24: [no test files]: github.com/smartystreets/goconvey
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/reports
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/fonts/Open_Sans
2018/12/21 16:57:39 parser.go:24: [passed]: github.com/smartystreets/goconvey/web/server/system
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/css/themes
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/fonts/FontAwesome/css
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/js
2018/12/21 16:57:39 parser.go:24: [no test functions]: github.com/smartystreets/goconvey/web/server/contract
2018/12/21 16:57:39 parser.go:24: [no test functions]: github.com/smartystreets/goconvey/web/server/messaging
2018/12/21 16:57:39 parser.go:24: [disabled]: github.com/smartystreets/goconvey/web/server/watch/integration_testing/sub
2018/12/21 16:57:39 parser.go:24: [passed]: github.com/smartystreets/goconvey/examples
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/fonts/Oswald
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/js/lib
2018/12/21 16:57:39 parser.go:24: [passed]: github.com/smartystreets/goconvey/convey
2018/12/21 16:57:39 parser.go:24: [no test functions]: github.com/smartystreets/goconvey/convey/gotest
2018/12/21 16:57:39 parser.go:24: [passed]: github.com/smartystreets/goconvey/web/server/executor
2018/12/21 16:57:39 parser.go:24: [no test functions]: github.com/smartystreets/goconvey/web/server/watch/integration_testing
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/fonts/Orbitron
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client
2018/12/21 16:57:39 parser.go:24: [passed]: github.com/smartystreets/goconvey/web/server/parser
2018/12/21 16:57:39 parser.go:24: [passed]: github.com/smartystreets/goconvey/convey/reporting
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/fonts/FontAwesome/fonts
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/ico
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/server
2018/12/21 16:57:39 parser.go:24: [passed]: github.com/smartystreets/goconvey/web/server/api
2018/12/21 16:57:39 parser.go:24: [passed]: github.com/smartystreets/goconvey/web/server/watch
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/css
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/fonts
2018/12/21 16:57:39 parser.go:24: [no go code]: github.com/smartystreets/goconvey/web/client/resources/fonts/FontAwesome
2018/12/21 16:57:39 executor.go:69: Executor status: 'idle'