文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
End To End Testing
End to end allows us to test applications through the whole request cycle. Where unit testing is meant to just test a particular function, end to end tests will run the middleware, router, and other that a request my pass through.
package main
import (
"fmt"
"net/http"
"github.com/codegangsta/negroni"
"github.com/julienschmidt/httprouter"
)
func HelloWorld(res http.ResponseWriter, req *http.Request, p httprouter.Params) {
fmt.Fprint(res, "Hello World")
}
func App() http.Handler {
n := negroni.Classic()
m := func(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
fmt.Fprint(res, "Before...")
next(res, req)
fmt.Fprint(res, "...After")
}
n.Use(negroni.HandlerFunc(m))
r := httprouter.New()
r.GET("/", HelloWorld)
n.UseHandler(r)
return n
}
func main() {
http.ListenAndServe(":3000", App())
}
This is the test file. It should be placed in the same directory as your application and name main_test.go
.
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func Test_App(t *testing.T) {
ts := httptest.NewServer(App())
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
exp := "Before...Hello World...After"
if exp != string(body) {
t.Fatalf("Expected %s got %s", exp, body)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论