测试golang的Web应用程序查询参数的最佳实践

huangapple go评论144阅读模式
英文:

Best practices for testing a golang's Web apps query parameters

问题

在两个必需参数的简单情况下,根据我了解,有四种可能的测试情况:

  1. 两个参数都为空
  2. 第一个参数设置但第二个参数未设置
  3. 第二个参数设置但第一个参数未设置
  4. 两个参数都设置

最佳实践是测试这四种情况吗?

因为即使在 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:

  1. both empty
  2. first set but not second
  3. second set but not first
  4. 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)
		}
	}
}

huangapple
  • 本文由 发表于 2017年9月14日 13:48:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/46211446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定