go 框架中的测试策略和指南包括:单元测试:孤立测试单个函数或方法,确保在不同输入下正常运行。集成测试:测试不同组件集成后的正常运行,涉及多个函数和类型。基准测试:衡量代码性能,通过测量在特定输入下的运行时间优化性能。模拟测试:用于测试依赖外部资源或 api 的代码,通过创建模拟对象在不依赖外部系统的情况下测试代码。
Go 框架中的测试策略和指南
单元测试
单元测试是针对单个函数或方法进行的隔离测试。它们确保在不同的输入下代码正常运行。使用 testing 包来编写单元测试:
import "testing"
func TestAdd(t *testing.T) {
result := Add(2, 3)
if result != 5 {
t.Errorf("Add(2, 3) = %d, want 5", result)
}
}
集成测试
集成测试确保不同的组件集成在一起时正常运行。它们涉及多个函数和类型。使用 context 和 testing 包来编写集成测试:
import (
"context"
"testing"
)
func TestHandler(t *testing.T) {
ctx := context.Background()
req := http.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()
Handler(ctx, rr, req)
if rr.Code != http.StatusOK {
t.Errorf("Handler returned %d, want %d", rr.Code, http.StatusOK)
}
}
基准测试
基准测试衡量代码的性能。它们通过测量代码在特定输入下的运行时间来帮助优化性能。使用 testing 包的 B 类型来编写基准测试:
import "testing"
func BenchmarkAdd(b *testing.B) {
for i := 0; i < b.N; i++ {
Add(2, 3)
}
}
模拟测试
模拟测试用于测试依赖外部资源或 API 的代码。它们通过创建模拟对象来测试代码而不依赖于外部系统。使用 mock 包来创建模拟对象:
import (
"testing"
"<a style='color:#f60; text-decoration:underline;' href="https://www.php.cn/zt/15841.html" target="_blank">git</a>hub.com/<a style='color:#f60; text-decoration:underline;' href="https://www.php.cn/zt/16009.html" target="_blank">golang</a>/mock/gomock"
)
func TestClient(t *testing.T) {
ctrl := gomock.NewController(t)
mockClient := mock_client.NewMockClient(ctrl)
client := &Client{MockClient: mockClient}
mockClient.EXPECT().Get(gomock.Any()).Return("Hello, world!", nil)
result, err := client.Get()
if err != nil {
t.Errorf("Client.Get() returned an error: %v", err)
}
if result != "Hello, world!" {
t.Errorf("Client.Get() returned %q, want %q", result, "Hello, world!")
}
}
实战案例
创建 API 服务器
package main
import (
"context"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/add", AddHandler).Methods("POST")
http.ListenAndServe(":8080", router)
}
func AddHandler(w http.ResponseWriter, r *http.Request) {
n1, err := strconv.Atoi(r.FormValue("n1"))
if err != nil {
http.Error(w, "Invalid n1 parameter", http.StatusBadRequest)
return
}
n2, err := strconv.Atoi(r.FormValue("n2"))
if err != nil {
http.Error(w, "Invalid n2 parameter", http.StatusBadRequest)
return
}
sum := n1 + n2
w.Write([]byte(strconv.Itoa(sum)))
}
测试 API 服务器
UnitTest
import (
"bytes"
"io/ioutil"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
)
func TestAddHandler(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/add", AddHandler).Methods("POST")
tests := []struct {
body string
want string
}{
{body: "", want: "Invalid n1 parameter"},
{body: "n1=1", want: "Invalid n2 parameter"},
{body: "n1=1&n2=2", want: "3"},
}
for _, test := range tests {
req := httptest.NewRequest("POST", "/add", bytes.NewBufferString(test.body))
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("AddHandler returned %d, want %d", rr.Code, http.StatusOK)
}
body, err := ioutil.ReadAll(rr.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
if string(body) != test.want {
t.Errorf("AddHandler returned %q, want %q", string(body), test.want)
}
}
}
IntegrationTest
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gorilla/mux"
)
func TestServer(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/add", AddHandler).Methods("POST")
ts := httptest.NewServer(router)
defer ts.Close()
tests := []struct {
url string
body string
want string
}{
{url: ts.URL + "/add", body: "", want: "Invalid n1 parameter"},
{url: ts.URL + "/add", body: "n1=1", want: "Invalid n2 parameter"},
{url: ts.URL + "/add", body: "n1=1&n2=2", want: "3"},
}
for _, test := range tests {
req, err := http.NewRequest("POST", test.url, strings.NewReader(test.body))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
rr := httptest.NewRecorder()
http.DefaultClient.Do(req)