Beego - 端点测试 [英] Beego - Endpoint Testing

查看:812
本文介绍了Beego - 端点测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为beego测试http定制端点

  package test 

import(
github.com/astaxie/beego
github.com/smartystreets/goconvey/convey
_golife-api-cons / routers
net / http
net / http / httptest
path / filepath
runtime
testing


func init {
_,file,_,_:= runtime.Caller(1)
apppath,_:= filepath.Abs​​(filepath.Dir(filepath.Join(file,..+ string( filepath.Separator))))
beego.TestBeegoInit(apppath)
}

// TestGet是运行端点测试的示例
func TestGet(t * testing.T){
r,_:= http.NewRequest(GET,/ my / endpoint / fetches / data,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)
})
})
}

当我运行去测试-v


$ b $我得到了响应 dial tcp:0:getsockopt:connection refused



我正在使用MariaDB运行在我的本地
我已经验证使用 netstat -tulpn ,我的数据库运行得很好(如果我使用邮递员并且服务器正在运行,我会得到一个有效的响应)


包含行 _golife-api-cons / routers后,我得到了一个奇怪的观察结果错误甚至在测试运行之前运行

我的测试通过响应200 OK,但没有任何数据,因为我得到的响应上述错误

编辑



使用的默认路径TestBeegoInit 使用的函数是 / path / to / my / project / test
其中是不是所需的路径,所以我试着给绝对路径,我仍然无法连接数据库。

很多尝试我都知道,beego在 beego / conf.go 中初始化它的名为 AppPath 的变量,比如 -

  AppPath,_ = filepath.Abs​​(filepath.Dir(os.Args [0]))

当你运行你的测试时,你用去测试-v



但结果是 os.Args [0] 是文本可执行文件,它将是 / tmp / path / to / test 而不是 path / to / app / exe



因此,它找不到 config / app.conf ,它位于具有db连接详细信息的应用程序路径中。
beego / conf.go 中的负责行 -
$ b

  appConfigPath = filepath.Join( AppPath,conf,app.conf)

这一切都发生在beego的<$ c $当你说

  import(
github.com/astaxie / beego
_path / to / routers

因为这是 -



用init函数创建一个新的包/文件,它看起来有 -

 包通用

导入(
os
字符串


func init(){
cwd:= os.Getenv(PWD)
rootDir:= strings.Split(cwd,tests /)
os.Args [0] = rootDir [0] //你的路径dir
}

这里你改变 os。 Args [0] 并指定目录路径

确保在beego之前导入它,所以现在导入将看起来像

 导入(
_path / to / common
github.com/astaxie/beego
_path / to / routers

最后连接到DB!


I am testing http custom endpoint for beego

package test

import (
    "github.com/astaxie/beego"
    . "github.com/smartystreets/goconvey/convey"
    _ "golife-api-cons/routers"
    "net/http"
    "net/http/httptest"
    "path/filepath"
    "runtime"
    "testing"
)

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", "/my/endpoint/fetches/data", 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)
        })
    })
}

When i run using go test -v ,

I get in response dial tcp :0: getsockopt: connection refused

I am using MariaDB running on my local, I have verified using netstat -tulpn that my database is running perfectly fine (I get a valid response if i use postman and my server is running)

One weird observation , after inclusion of line _ "golife-api-cons/routers" i get this error even before test's are ran

My test passes with response 200 OK , but without any data as i get in response the above mentioned error

EDIT

The default path by used by TestBeegoInit function used is /path/to/my/project/test which is not the desired path , so i tried giving the absolute path too , still i am not able to connect DB.

解决方案

After much trying I came to know that beego initializes its variable called as AppPath in beego/conf.go like -

AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))

when you run your tests you run them with go test -v

but as a result the os.Args[0] is the text executable which will be /tmp/path/to/test and not path/to/app/exe

hence as a result it does not find config/app.conf which is in your app path which has db connection details. Responsible line in beego/conf.go -

appConfigPath = filepath.Join(AppPath, "conf", "app.conf")

This all happens in beego's init function when you say

import (
  "github.com/astaxie/beego"
  _ "path/to/routers"
)

Hack for this is -

create a new package / file with init function which looks has -

package common

import (
    "os"
    "strings"
)

func init() {
    cwd := os.Getenv("PWD")
    rootDir := strings.Split(cwd, "tests/")
    os.Args[0] = rootDir[0] // path to you dir
}

here you are changing os.Args[0] and assigning your directory path

make sure you import it before beego so now import will look like

import (
  _ "path/to/common"
  "github.com/astaxie/beego"
  _ "path/to/routers"
)

And finally you connect to DB !

这篇关于Beego - 端点测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆