具体可以参考: zlsgo-app 项目
HTTP 测试
// main.go package main import ( "net/http" "github.com/sohaha/zlsgo/znet" ) func main() { r := znet.New() r.GET("/ping", func(c *znet.Context) { c.String(http.StatusOK, "ok") }) znet.Run() }
// main_test.go package main import ( "io" "net/http" "net/http/httptest" "os" "testing" . "github.com/sohaha/zlsgo" "github.com/sohaha/zlsgo/znet" ) var net *znet.Engine func request(method, url string, body io.Reader) *httptest.ResponseRecorder { w := httptest.NewRecorder() req, _ := http.NewRequest(method, url, body) net.ServeHTTP(w, req) return w } func TestMain(m *testing.M) { // 获取服务, 即 *znet.Engine // 需注册好路由先 net = initEngine() // 运行测试 s := m.Run() os.Exit(s) } func TestHome(t *testing.T) { tt := NewTest(t) w := request("GET", "/ping", nil) tt.EqualExit(200, w.Code) tt.EqualExit("ok", w.Body.String()) t.Log("测试通过") }
# 运行测试 go test -v