GoFiber框架之测试应用

大家好,我是 polarisxu。

实际项目中,大家经常不会对 Web API 写单元测试。Go 标准库不仅有 testing 包支持普通单元测试,还有 net/http/httptest 包支持 HTTP 的测试。

本文虽然是测试 Fiber 应用程序,但对其他的框架也适用。

01 如何测试

Web API 的单元测试如何进行?

本节介绍的测试方法主要是验证请求返回的 HTTP 状态码是否符合预期。

如果返回的状态码是 200 OK,那么表示这个测试用例成功(Pass),如果返回的状态码是 404 Not Found,那么表示这个测试用例失败(Fail)。所以,要求请求返回正确的状态码。

02 VSCode 生成测试

VSCode 安装了 Go Team 的 Go 插件后,可以一键生成单元测试。

在某个函数上右键,出现的菜单中会有 Generate Unit Tests For Function:

点击它会自动创建 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 总结

本文从 HTTP 状态码的维度测试 Web API,保证 API 大的逻辑正确,但不包括业务逻辑相关的测试。

本文转载自微信公众号「polarisxu」,可以通过以下二维码关注。转载本文请联系polarisxu公众号。

当前文章:GoFiber框架之测试应用
转载来源:http://www.shufengxianlan.com/qtweb/news42/317692.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联