golang分层测试之http接口测试入门教程

2019-11-10 12:25:41王旭

带json的post请求

我们大部分应用到的restful接口都是用json格式的请求体,对应的golang的http请求也会有相关的方式post json请求体

package main

import (
 "fmt"
 "io/ioutil"
 "net/http"
  "bytes"
 "encoding/json"
)


type HttpData struct {

 Flag int `json:"flag"`
 Msg string `json:"msg"`

}

func main() {

 url := "http://127.0.0.1:12345/postdata"
 contentType := "application/json;charset=utf-8"

 var httpdata HttpData
 httpdata.Flag = 1
 httpdata.Msg = "terrychow"

 
 b ,err := json.Marshal(httpdata)
 if err != nil {
  fmt.Println("json format error:", err)
  return
 }

 body := bytes.NewBuffer(b)

 resp, err := http.Post(url, contentType, body)
 if err != nil {
  fmt.Println("Post failed:", err)
  return
 }

 defer resp.Body.Close()


 content, err := ioutil.ReadAll(resp.Body)
 if err != nil {
  fmt.Println("Read failed:", err)
  return
 }

 fmt.Println("header:", resp.Header)
 fmt.Println("content:", string(content))

}

执行结果响应

E:go_project>go run gohttptest.go
header: map[Content-Type:[application/json] Content-Length:[78] Server:[Werkzeug/0.14.1 Python/2.7.15] Date:[Thu, 06 Dec 2018 16:35:11 GMT]]
content: {
 "code": 200,
 "data": 1,
 "msg": "terrychow",
 "state": "success"
}

对于常用的get和post请求基本上就以照上面的版本执行,当然我们现在需要做的是http接口的测试,那就需要引入测试框架进行相关的校验,本文先讲解用之前提到的gocheck来进行断言

golang中的http接口测试

引入gocheck之后我们得到了以下的脚本:

package hello_test

import (
 "testing"
 "fmt"
 "strconv"
 "io/ioutil"
 "net/http"
  "bytes"
 "encoding/json"
 . "gopkg.in/check.v1"
)

var a int =1


// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type MySuite struct{}

type HttpData struct {

 Flag int `json:"flag"`
 Msg string `json:"msg"`

}

var _ = Suite(&MySuite{})

var testurl string ="http://127.0.0.1:12345"

func (s *MySuite) SetUpSuite(c *C) {
 str3:="第1次套件开始执行"
 fmt.Println(str3)
 //c.Skip("Skip TestSutie")
}

func (s *MySuite) TearDownSuite(c *C) {
 str4:="第1次套件执行完成"
 fmt.Println(str4)
}

func (s *MySuite) SetUpTest(c *C) {
 str1:="第"+strconv.Itoa(a)+"条用例开始执行"
 fmt.Println(str1)

}

func (s *MySuite) TearDownTest(c *C) {
 str2:="第"+strconv.Itoa(a)+"条用例执行完成"
 fmt.Println(str2)
 a=a+1
}

func (s *MySuite) TestHttpGet(c *C) {
 geturl := fmt.Sprintf("%v/checkon", testurl)
 respget, err := http.Get(geturl)
 if err != nil {
  panic(err)
 }
 defer respget.Body.Close() //关闭连接

 body, err := ioutil.ReadAll(respget.Body) //读取body的内容
 var gdat map[string]interface{} //定义map用于解析resp.body的内容
 if err := json.Unmarshal([]byte(string(body)), &gdat); err == nil {
  fmt.Println(gdat)
 } else {
  fmt.Println(err)
 }
 var gmsg=gdat["msg"]
 c.Assert(gmsg, Equals, "terrychow") //模拟失败的断言

}

func (s *MySuite) TestHttpPost(c *C) {

 url := fmt.Sprintf("%v/postdata", testurl)
 contentType := "application/json;charset=utf-8"

 var httpdata HttpData
 httpdata.Flag = 1
 httpdata.Msg = "terrychow"

 
 b ,err := json.Marshal(httpdata)
 if err != nil {
  fmt.Println("json format error:", err)
  return
 }

 body := bytes.NewBuffer(b)

 resp, err := http.Post(url, contentType, body)
 if err != nil {
  fmt.Println("Post failed:", err)
  return
 }

 defer resp.Body.Close()

 content, err := ioutil.ReadAll(resp.Body)
 if err != nil {
  fmt.Println("Read failed:", err)
  return
 }
 var dat map[string]interface{} //定义map用于解析resp.body的内容
 if err := json.Unmarshal([]byte(string(content)), &dat); err == nil {
  fmt.Println(dat)
 } else {
  fmt.Println(err)
 }
 var msg=dat["msg"]
 c.Assert(msg, Equals, "terrychow") //模拟成功的断言
}