package main
import (
"fmt"
"math"
)
// debugging sucks! Testing Rocks!
/*
传统测试
缺点{
1.测试数据和测试逻辑混在一起
2.出错信息不明确
3.一旦一个数据出错测试全部结束
}
表格驱动测试
优点{
1.分离的测试数据和测试逻辑
2.明确的出错信息
3.可以部分失败
4.go语言的语法使得我们更易实践表格驱动测试
}
*/
func triangle() {
var a, b int = 3, 4
fmt.Println(calcTriangle(a, b))
}
func calcTriangle(a, b int) int {
var c int
c = int(math.Sqrt(float64(a*a + b*b)))
return c
}
func main() {
fmt.Println("1111")
fmt.Println("2222")
fmt.Println("3333")
}
package main
import "testing"
/*
启动测试命令
运行:在当前路径下面 go test .
测试覆盖率:go test -coverprofile=c.out // windows 下测试失败
benchmark启动: go test -bench .
查看性能:go test -bench . -cpuprofile cpu.out
使用pprof查看代码性能:go tool pprof cpu.out
*/
// 功能测试
func TestSubstr(t *testing.T) {
tests := []struct {
s string
ans int
}{
// Normal cases
{"abcabcbb", 3},
{"pwwkew", 3},
// Edge cases
{"0", 1},
{"b", 1},
{"bbbbbbbbbb", 1},
{"abcabcabcd", 4},
// chinese support
{"这里是慕课网", 6},
{"一二三二一", 3},
{"黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花", 8},
}
for _, tt := range tests {
if actual := lengthOfNonRepeatingSubStr(tt.s); actual != tt.ans {
t.Errorf("got %d for input %s;"+"expected %d", actual, tt.s, tt.ans)
}
}
}
// 性能测试 map runtime
func BenchmarkSubstr(b *testing.B) {
s := "黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花"
ans := 8
for i := 0; i < b.N; i++ {
if actual := lengthOfNonRepeatingSubStr(s); actual != ans {
b.Errorf("got %d for input %s;"+"expected %d", actual, s, ans)
}
}
}
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
/*
http测试
1.通过使用假的Request/Response 单元测试
2.通过起服务器
*/
func errPanic(writer http.ResponseWriter, request *http.Request) error {
panic(123)
}
type testingUserError string
func (e testingUserError) Error() string {
return e.Message()
}
func (e testingUserError) Message() string {
return string(e)
}
func errUserError(writer http.ResponseWriter, request *http.Request) error {
return testingUserError("user error")
}
func errNotFound(writer http.ResponseWriter, request *http.Request) error {
return os.ErrNotExist
}
func errNoPermission(writer http.ResponseWriter, request *http.Request) error {
return os.ErrPermission
}
func errUnknown(writer http.ResponseWriter, request *http.Request) error {
return errors.New("unknown error")
}
func noError(writer http.ResponseWriter, request *http.Request) error {
fmt.Fprintln(writer, "no error")
return nil
}
var tests = []struct {
h appHandler
code int
message string
}{
{errPanic, 500, "Internal Server Error"},
{errUserError, 400, "user error"},
{errNotFound, 404, "Not Found"},
{errNoPermission, 403, "Forbidden"},
{errUnknown, 500, "Internal Server Error"},
{noError, 200, "no error"},
}
func verifyResponse(resp *http.Response, expectedCode int, expectedMsg string, t *testing.T) {
b, _ := ioutil.ReadAll(resp.Body)
body := strings.Trim(string(b), "\n")
if resp.StatusCode != expectedCode || body != expectedMsg {
t.Errorf("expect (%d, %s); "+"got(%d, %s)", expectedCode, expectedMsg, resp.StatusCode, body)
}
}
func TestErrWrapper(t *testing.T) {
for _, tt := range tests {
f := errWrapper(tt.h)
response := httptest.NewRecorder()
request := httptest.NewRequest(
http.MethodGet,
"http://www.imooc.com", nil)
f(response, request)
verifyResponse(response.Result(), tt.code, tt.message, t)
}
}
func TestErrWrapperInServer(t *testing.T) {
for _, tt := range tests {
f := errWrapper(tt.h)
server := httptest.NewServer(http.HandlerFunc(f))
resp, _ := http.Get(server.URL)
verifyResponse(resp, tt.code, tt.message, t)
}
}