自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go Fiber 框架之測試應(yīng)用

開發(fā) 后端
實際項目中,大家經(jīng)常不會對 Web API 寫單元測試。Go 標準庫不僅有 testing 包支持普通單元測試,還有 net/http/httptest 包支持 HTTP 的測試。

[[428408]]

大家好,我是 polarisxu。

實際項目中,大家經(jīng)常不會對 Web API 寫單元測試。Go 標準庫不僅有 testing 包支持普通單元測試,還有 net/http/httptest 包支持 HTTP 的測試。

本文雖然是測試 Fiber 應(yīng)用程序,但對其他的框架也適用。

01 如何測試

Web API 的單元測試如何進行?

本節(jié)介紹的測試方法主要是驗證請求返回的 HTTP 狀態(tài)碼是否符合預(yù)期。

如果返回的狀態(tài)碼是 200 OK,那么表示這個測試用例成功(Pass),如果返回的狀態(tài)碼是 404 Not Found,那么表示這個測試用例失敗(Fail)。所以,要求請求返回正確的狀態(tài)碼。

02 VSCode 生成測試

VSCode 安裝了 Go Team 的 Go 插件后,可以一鍵生成單元測試。

在某個函數(shù)上右鍵,出現(xiàn)的菜單中會有 Generate Unit Tests For Function:

點擊它會自動創(chuàng)建 main_test.go 文件,并生成類似下面的代碼:

  1. package main 
  2.  
  3. import "testing" 
  4.  
  5. func Test_main(t *testing.T) { 
  6.  tests := []struct { 
  7.   name string 
  8.  }{ 
  9.   // TODO: Add test cases. 
  10.  } 
  11.  for _, tt := range tests { 
  12.   t.Run(tt.name, func(t *testing.T) { 
  13.    main() 
  14.   }) 
  15.  } 

03 動手寫單元測試

動手之前,需要先介紹下 Fiber 中專門針對測試提供的方法:

  1. // Test is used for internal debugging by passing a *http.Request. 
  2. // Timeout is optional and defaults to 1s, -1 will disable it completely. 
  3. func (app *App) Test(req *http.Request, msTimeout ...int) (resp *http.Response, err error) 

該方法接收一個 *http.Request,返回 *http.Response,通過這個 Response 可以獲得 HTTP StatusCode。

待測試的程序如下:

  1. // main.go 
  2. package main 
  3.  
  4. import ( 
  5.  "github.com/gofiber/fiber/v2" 
  6.  
  7. func setupRoutes(app *fiber.App) { 
  8.  app.Get("/hello", func(ctx *fiber.Ctx) error { 
  9.   return ctx.SendString("Hello World!"
  10.  }) 
  11.  
  12. func main() { 
  13.  app := fiber.New() 
  14.  setupRoutes(app) 
  15.  app.Listen(":3000"

測試程序如下:

  1. package main 
  2.  
  3. import ( 
  4.  "net/http/httptest" 
  5.  "testing" 
  6.  
  7.  "github.com/gofiber/fiber/v2" 
  8.  "github.com/stretchr/testify/assert" 
  9.  
  10. func TestHelloRoute(t *testing.T) { 
  11.  tests := []struct { 
  12.   description  string 
  13.   route        string // route path to test 
  14.   expectedCode int    // expected HTTP status code 
  15.  }{ 
  16.   { 
  17.    description:  "get HTTP status 200"
  18.    route:        "/hello"
  19.    expectedCode: 200, 
  20.   }, 
  21.   { 
  22.    description:  "get HTTP status 404, when route is not exists"
  23.    route:        "/notfound"
  24.    expectedCode: 404, 
  25.   }, 
  26.  } 
  27.  
  28.  app := fiber.New() 
  29.  
  30.  setupRoutes(app) 
  31.  
  32.  for _, test := range tests { 
  33.   // 利用 httptest 包生成 request 
  34.   req := httptest.NewRequest("GET", test.route, nil) 
  35.   resp, _ := app.Test(req, 1) 
  36.   assert.Equalf(t, test.expectedCode, resp.StatusCode, test.description) 
  37.  } 

我們還用了 github.com/stretchr/testify 庫,這是一個輔助測試的庫,assert 是它的子包,用于進行斷言。

然后運行如下命令測試:

  1. $ go test -v . 
  2. === RUN   TestHelloRoute 
  3. --- PASS: TestHelloRoute (0.00s) 
  4. PASS 
  5. ok   github.com/polaris1119/fiber-example 

04 總結(jié)

 

本文從 HTTP 狀態(tài)碼的維度測試 Web API,保證 API 大的邏輯正確,但不包括業(yè)務(wù)邏輯相關(guān)的測試。

本文轉(zhuǎn)載自微信公眾號「polarisxu」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系polarisxu公眾號。

 

責(zé)任編輯:武曉燕 來源: polarisxu
相關(guān)推薦

2021-10-06 19:03:35

Go中間件Middleware

2024-01-04 07:02:36

GoLangFiber開發(fā)

2021-09-26 05:05:46

GoFiber Express

2023-05-18 14:01:00

前端自動化測試

2024-12-12 08:57:47

2022-02-09 14:36:25

GoMongoDBFiber

2022-04-08 09:01:56

腳本Go應(yīng)用單元

2023-10-22 20:20:37

FiberGo

2021-06-26 07:40:21

前端自動化測試Jest

2022-07-13 15:23:57

Vue fiberreact前端

2013-09-02 16:08:50

調(diào)試Windows

2022-01-07 15:11:27

項目Go 框架

2023-12-01 09:14:58

ReactFiber

2009-11-25 10:57:17

2014-10-15 11:01:02

Web應(yīng)用測試應(yīng)用

2023-07-13 08:06:05

應(yīng)用協(xié)程阻塞

2022-10-27 18:03:04

GogRPC云原生

2022-04-27 08:17:07

OCMock單元測試集成

2023-01-12 08:00:00

SpringClou微服務(wù)框架

2010-08-27 09:11:27

Python單元測試
點贊
收藏

51CTO技術(shù)棧公眾號