英文:
Best practices for testing a golang's Web apps query parameters
问题
在两个必需参数的简单情况下,根据我了解,有四种可能的测试情况:
- 两个参数都为空
- 第一个参数设置但第二个参数未设置
- 第二个参数设置但第一个参数未设置
- 两个参数都设置
最佳实践是测试这四种情况吗?
因为即使在 Golang 中测试第一个和最后一个情况也相当冗长:
func TestGoodParameter(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
q := req.URL.Query()
q.Add("first", "foo")
q.Add("second", "bar")
req.URL.RawQuery = q.Encode()
rec := httptest.NewRecorder()
root(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Errorf("got %v, expected %v", res.StatusCode, http.StatusOK)
}
}
func TestBadParameter(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
rec := httptest.NewRecorder()
root(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusBadRequest {
t.Errorf("got %v, expected %v", res.StatusCode, http.StatusBadRequest)
}
}
或者我在这里漏掉了一些技巧吗?当有五个参数时,其中两个是可选的时,显然情况会变得更加复杂!
英文:
In a simple case of two required parameters, there are four possible test cases IIUC:
- both empty
- first set but not second
- second set but not first
- both set
What is best practice, test all four cases?
Because even testing the first and last cases is quite verbose in Golang:
func TestGoodParameter(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
q := req.URL.Query()
q.Add("first", "foo")
q.Add("second", "bar")
req.URL.RawQuery = q.Encode()
rec := httptest.NewRecorder()
root(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Errorf("got %v, expected %v", res.StatusCode, http.StatusOK)
}
}
func TestBadParameter(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
rec := httptest.NewRecorder()
root(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusBadRequest {
t.Errorf("got %v, expected %v", res.StatusCode, http.StatusBadRequest)
}
}
Or are there some tricks I am missing here? It obviously gets even more complicated when say there are five parameters where two of them are optional!
答案1
得分: 11
使用表驱动方式定义测试用例,并为其编写单一实现。您可以通过省略测试用例的名称来简化定义。
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestParameters(t *testing.T) {
testCases := map[string]struct {
params map[string]string
statusCode int
}{
"good params": {
map[string]string{
"first": "foo",
"second": "bar",
},
http.StatusOK,
},
"without params": {
map[string]string{},
http.StatusBadRequest,
},
}
for tc, tp := range testCases {
req, _ := http.NewRequest("GET", "/", nil)
q := req.URL.Query()
for k, v := range tp.params {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
rec := httptest.NewRecorder()
root(rec, req)
res := rec.Result()
if res.StatusCode != tp.statusCode {
t.Errorf("`%v` failed, got %v, expected %v", tc, res.StatusCode, tp.statusCode)
}
}
}
希望对您有所帮助!
英文:
Define your test cases table driven way and write a single implementation for it. You can simplify definition by omitting the name of test cases.
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestParameters(t *testing.T) {
testCases := map[string]struct {
params map[string]string
statusCode int
}{
"good params": {
map[string]string{
"first": "foo", "second": "bar",
},
http.StatusOK,
},
"without params": {
map[string]string{},
http.StatusBadRequest,
},
}
for tc, tp := range testCases {
req, _ := http.NewRequest("GET", "/", nil)
q := req.URL.Query()
for k, v := range tp.params {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
rec := httptest.NewRecorder()
root(rec, req)
res := rec.Result()
if res.StatusCode != tp.statusCode {
t.Errorf("`%v` failed, got %v, expected %v", tc, res.StatusCode, tp.statusCode)
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论