Issue in testing web app

blov · 2017-03-05 13:00:10 · 644 次点击    
这是一个分享于 2017-03-05 13:00:10 的资源,其中的信息可能已经有所发展或是发生改变。

When I am writing my test case, I am using a views_test.go file in the views package, yet, the test cases says "function not found", the function is a part of this package and it is unable to find it, when I try importing the views package unto itself (I know this is a cycling import, only then it works) it works file if I run go test views/views_test.go but fails at go test ./views/

    func TestWrongMethodSearchHandler(t *testing.T) {
            token :=   "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN1cmFqIiwiZXhwIjoxNDg4NzA0MTg5fQ.eZKNxaOBJv0oRyFKZR5vF8u3xqKDvuGB_KAWsB2ElBI"

    ts := httptest.NewServer(http.HandlerFunc(SearchHandler))
    defer ts.Close()

    testCases := []struct {
            method     string
            wantStatus int
    }{
            {
                    "GET",
                    http.StatusOK,
            },
    }

    for _, tc := range testCases {

            req, err := http.NewRequest(tc.method, ts.URL, nil)
            req.Form = make(map[string][]string, 0)
            req.Form.Add("name", "business")
            if err != nil {
                    t.Fatal(err)
            }
            req.Header.Set("token", token)

            rr := httptest.NewRecorder()
            SearchHandler(rr, req)

            if status := rr.Code; status != tc.wantStatus {
                    t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)

            }
    }
}

**评论:**

neopointer:

It would be better if you post the code. But the only thing that I can think of right now is that you've given a different package name in the test file. Like both files are in the same folder, but instead of both having package name views, the test file has package name views_test, and then from this package you can't access the unexported functions of the views package.

thewhitetulip:

That's what is strange, both files normal and test case have the same package name "views". Still it throws that error! I have updated the question to contain the handler test case.

After I get this to work, it says table not found for each SQL query, I am going nuts on this issue, can't seem to solve it.

neopointer:

That should be easy to track. It seems you are not connecting to the right database then...

thewhitetulip:

Now this is where I don't understand where things are headed! I am writing an app using sqlite3, I don't know which db the server is referring to!

thewhitetulip:

Can you show me an example unit test case about a web server? I didn't find a single document on the web which teaches how to test a webserver, I am writing a RESTful API.

neopointer:

Honestly I don't have much experience with testing in go. So I would suggest that you check how you are constructing your db connection to know where are you connecting to. It could be that are using an in-memory database connection with sqlite, so it will always be empty.

thewhitetulip:

I found out now that I am testing from inside the views directory, this it created a new sqlite database in my filepath, testing with that code now! I am still stumped as to why I have to import views inside the views package, it is strange.

thewhitetulip:
    views $  go test
    2017/03/05 13:08:18 map[name:[business]]
    2017/03/05 13:08:18 business
    2017/03/05 13:08:18 searching for product name  business
    2017/03/05 13:08:18 map[name:[business]]
    PASS
    ok      github.com/thewhitetulip/views  0.068s
    views $  go test views_test.go
    # command-line-arguments
    ./views_test.go:12: undefined: SearchHandler
    ./views_test.go:36: undefined: SearchHandler
    ./views_test.go:47: undefined: SearchHandler
    ./views_test.go:59: undefined: SearchHandler
    FAIL    command-line-arguments [build failed]
    ```

Okay, so I figured out the issue, I wasn't doing testing properly, I did go test views_test.go instead of go test, by doing go test it works fine!!

neopointer:

You can also use go test ./... from the root folder (it will test everything). Happy coding :)

thewhitetulip:

Thank you for your help!!

tmornini:

Shameless self promotion:

Take a look at github.com/tmornini/http-spec

I wrote it to test APIs via HTTP.

It's fast, written in go, and is being actively used and developed.

thewhitetulip:

Thanks! For what it's worth, I don't see this as shameless self promotion, if not for self promotion how are you going to spread word about what you built? :-)

Sure some might detest this, but let's not get swayed by a few such folks, let's do good work and spread the word about it!


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

644 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传