打赏

Golang test

(1.)单元测试性能测试
https://github.com/guyan0319/golang_development_notes/blob/master/zh/1.9.md
(2.)gotests使用
https://github.com/cweill/gotests
(3.)Goland插件
https://www.jetbrains.com/help/go/creating-run-debug-configuration-for-tests.html#test-configuration-for-a-package
(4.) echo框架测试

import (
    "net/http"
    "net/http/httptest"
    "strings"
    "testing"

    "github.com/labstack/echo"
    "github.com/stretchr/testify/assert"
)

var userJSON = `{"name":"Jhon Doe","address":"High St."}`

func TestModel(t *testing.T) {
    url := "/new_house"
    e := echo.New()
    req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(userJSON))
    req.Header.Set("Content-Type", "application/json")
    if err != nil {
        t.Errorf("The request could not be created because of: %v", err)
    }
    rec := httptest.NewRecorder()
    c := e.NewContext(req, rec)
    // c.SetPath("/new_house")
    // c.JSON(http.StatusOK, Devices{"Jhon Doe", "Middle Way"})

    res := rec.Result()
    defer res.Body.Close()

    if assert.NoError(t, newHouse(c)) {
        assert.Equal(t, http.StatusOK, rec.Code)
        assert.Equal(t, "["+userJSON+"]", rec.Body.String())
    }
}

https://echo.labstack.com/guide/testing
https://github.com/labstack/echo/blob/master/echo_test.go

posted @ 2020-07-02 08:45  苍山落暮  阅读(141)  评论(0编辑  收藏  举报