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:
thewhitetulip: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 nameviews_test
, and then from this package you can't access the unexported functions of theviews
package.
neopointer: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.
thewhitetulip: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!
neopointer: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.
thewhitetulip: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.
neopointer: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 ofgo test
, by doinggo test
it works fine!!
thewhitetulip:You can also use
go test ./...
from the root folder (it will test everything). Happy coding :)
tmornini:Thank you for your help!!
thewhitetulip: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.
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!
