GUIDE(指引) - 测试

优质
小牛编辑
127浏览
2023-12-01

测试

测试业务逻辑

GET /users/:id

这个业务是根据用户的 id 从数据库取到该用户数据,如果用户不存在则返回404和提示语句。

创建 User

POST /users

  • 接受 JSON 格式的数据。
  • 创建成功返回 201 - Created
  • 发生错误返回 500 - Internal Server Error

获取 User

GET /users/:email

  • 获取成功返回 200 - OK
  • 发生错误返回 404 - Not Found if user is not found otherwise 500 - Internal Server Error

handler.go

  1. package handler
  2. import (
  3. "net/http"
  4. "github.com/labstack/echo"
  5. )
  6. type (
  7. User struct {
  8. Name string `json:"name" form:"name"`
  9. Email string `json:"email" form:"email"`
  10. }
  11. handler struct {
  12. db map[string]*User
  13. }
  14. )
  15. func (h *handler) createUser(c echo.Context) error {
  16. u := new(User)
  17. if err := c.Bind(u); err != nil {
  18. return err
  19. }
  20. return c.JSON(http.StatusCreated, u)
  21. }
  22. func (h *handler) getUser(c echo.Context) error {
  23. email := c.Param("email")
  24. user := h.db[email]
  25. if user == nil {
  26. return echo.NewHTTPError(http.StatusNotFound, "user not found")
  27. }
  28. return c.JSON(http.StatusOK, user)
  29. }

handler_test.go

  1. package handler
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. "github.com/labstack/echo"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. var (
  11. mockDB = map[string]*User{
  12. "jon@labstack.com": &User{"Jon Snow", "jon@labstack.com"},
  13. }
  14. userJSON = `{"name":"Jon Snow","email":"jon@labstack.com"}`
  15. )
  16. func TestCreateUser(t *testing.T) {
  17. // 设置
  18. e := echo.New()
  19. req := httptest.NewRequest(echo.POST, "/", strings.NewReader(userJSON))
  20. req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
  21. rec := httptest.NewRecorder()
  22. c := e.NewContext(req, rec)
  23. h := &handler{mockDB}
  24. // 断言
  25. if assert.NoError(t, h.createUser(c)) {
  26. assert.Equal(t, http.StatusCreated, rec.Code)
  27. assert.Equal(t, userJSON, rec.Body.String())
  28. }
  29. }
  30. func TestGetUser(t *testing.T) {
  31. // 设置
  32. e := echo.New()
  33. req := httptest.NewRequest(echo.GET, "/", nil)
  34. rec := httptest.NewRecorder()
  35. c := e.NewContext(req, rec)
  36. c.SetPath("/users/:email")
  37. c.SetParamNames("email")
  38. c.SetParamValues("jon@labstack.com")
  39. h := &handler{mockDB}
  40. // 断言
  41. if assert.NoError(t, h.getUser(c)) {
  42. assert.Equal(t, http.StatusOK, rec.Code)
  43. assert.Equal(t, userJSON, rec.Body.String())
  44. }
  45. }

使用 Form 表单提交

  1. f := make(url.Values)
  2. f.Set("name", "Jon Snow")
  3. f.Set("email", "jon@labstack.com")
  4. req := httptest.NewRequest(echo.POST, "/", strings.NewReader(f.Encode()))
  5. req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm)

设置 URL 参数

  1. c.SetParamNames("id", "email")
  2. c.SetParamValues("1", "jon@labstack.com")

设置请求参数

  1. q := make(url.Values)
  2. q.Set("email", "jon@labstack.com")
  3. req := http.NewRequest(echo.POST, "/?"+q.Encode(), nil)

测试中间件

TBD
你可以在这里查看框架自带中间件的测试代码。