如何用Beego制作API测试文件 [英] How to make API test files with Beego
问题描述
软件包测试
导入(
net / http
net / http / httptest
testing
runtime
path / filepath
_hello / routers
_github.com/lib/pq
github.com/astaxie/beego
。github.com/smartystreets/goconvey/convey
)
func init(){
_,file,_,_:= runtime.Caller(1)
apppath,_:= filepath.Abs(filepath.Dir(文件路径.join(file,..+ string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}
// TestGet是一个运行示例一个端点测试
func TestGet(t * testing.T){
r,_:= http.NewRequest(GET,/ api / testing,nil)
w:= httptest。 NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w,r)
beego.Trace(testing,TestGet,Code [%d] \\\
%s ,w.Code,w.Body.String())
Convey(Subject:Test Station Endpoint \\\
,t,func(){
Convey(状态代码应该是200,func(){
So(w.Code,ShouldEqual,200)
})
Convey(结果不应该为空,func (){
So(w.Body.Len(),ShouldBeGreaterThan,0)
})
})
}
然后当我运行 go test tests / *。go
我得到:
2015/01/14 23:55:19 [config.go:284] [W]打开/home/IdeaProjects/go/src/hello/tests/conf/app.conf:没有这样的文件或目录
[ORM] register db Ping'default`,pq:密码认证失败,用户 hello
必须有一个寄存器DataBase别名命名为`default`
FAIL命令行参数0.008s
我用 bee api
引导了Beego,然后使用pq Postgres Driver作为PG数据库。
另外,我不确定它为什么要查看 app.c的
文件它应该寻找 /hello/tests/conf/app.conf
路径onf /hello/conf/app.conf
。
是否有这样的原因?
PG无法验证您的身份,因为它无法找到密码 app.conf
文件,它也找不到,因为它在错误的位置。
试试使用绝对路径到你的配置,而不是你在 init()
函数中使用的复杂方法。
I have below test file:
package tests
import (
"net/http"
"net/http/httptest"
"testing"
"runtime"
"path/filepath"
_ "hello/routers"
_ "github.com/lib/pq"
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
)
func init() {
_, file, _, _ := runtime.Caller(1)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}
// TestGet is a sample to run an endpoint test
func TestGet(t *testing.T) {
r, _ := http.NewRequest("GET", "/api/testing", nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestGet", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", t, func() {
Convey("Status Code Should Be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The Result Should Not Be Empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0)
})
})
}
Then when I run go test tests/*.go
I get:
2015/01/14 23:55:19 [config.go:284] [W] open /home/IdeaProjects/go/src/hello/tests/conf/app.conf: no such file or directory
[ORM]register db Ping `default`, pq: password authentication failed for user "hello"
must have one register DataBase alias named `default`
FAIL command-line-arguments 0.008s
I've bootstraped Beego with bee api
then using pq Postgres Driver for PG database.
Also I'm not sure why it's looking at /hello/tests/conf/app.conf
path for app.conf
file it should look for /hello/conf/app.conf
.
Is there a reason why this is happening?
PG can't authenticate you probably because it can't find the password in the app.conf
file, which it also can't find because it's looking in the wrong place.
Try using an absolute path to your config rather than the convoluted method you have in your init()
function.
这篇关于如何用Beego制作API测试文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!