4

go测试库之apitest - 虫师

 1 year ago
source link: https://www.cnblogs.com/fnng/p/17375690.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

go测试库之apitest

2023-05-05 23:29  虫师  阅读(46)  评论(0)  编辑  收藏  举报

使用go语言做开发差不多快一年了,主要用来写后端Web服务,从一开始吐槽他的结构体,比如创建个复杂的JSON格式数据,那是相当的痛苦。还有 err 处理写的巨麻烦。

当然,go 也有爽的地方,创建个线协程简直太简单了。

到后来慢慢接受,觉得效率还行,因为是静态强类型语言,在修改完项目代码之后,反而很有信心(如果出现低级的类型错误,直接编译出错了),相比 Python 就要反复检查两边,对修改的代码总时心里发虚。

go语言测试相关的东西都不咋地,比如自带的测试框架相比较 pytest 那是相当的简陋。今年开始给后端写单元测试(其实应该叫接口测试),发现 apitest库 眼前一亮。采用链式调用,和 HttpRunner 3.x 的链式调用颇有几分相似。

  • HttpRunner 3.x
# httprunner 3.x 
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase


class TestCaseTestCase(HttpRunner):

    config = Config("basic test config").base_url("http://127.0.0.1:8000/api")

    teststeps = [
        Step(
            RunRequest(" test_add_event_all_null")
            .post("/add_event/")
            .with_data({"eid": "", "limit": "", "address": "", "start_time": ""})
            .validate()
            .assert_equal("body.status", 10021)
            .assert_equal("body.message", "parameter error")
        )
    ]


if __name__ == "__main__":
    TestCaseTestCase().test_start()

apitest 测试库

一个简单且可扩展的行为测试库。

测试库: https://github.com/steinfletcher/apitest jsonpath库: github.com/steinfletcher/apitest-jsonpath

  • 简单的get接口
package api

import (
 "net/http"
 "testing"
 "time"

 "github.com/steinfletcher/apitest"
)

func Client() http.Client {
 cli := &http.Client{
  Timeout: time.Second * 10,
 }
 return *cli
}

func TestGetSample(t *testing.T) {
 cli := Client()
 apitest.New().
    EnableNetworking(&cli).
  Get("http://httpbin.org/get").
  Expect(t).
  Status(http.StatusOK).
  End()
}

New(): 创建一个新的API测试。

EnableNetworking(): EnableNetworking为提供的客户端启用网络,需要一个 http.Clinet。

Get(): 发送get 请求,需要一个URL。

Expect(): Expect将请求规范标记为完整。

Status(): 断言http状态。http.StatusOK = 200

End(): End运行测试,将结果返回给调用者。

  • get接口带参数
import (
  ...
 jsonpath "github.com/steinfletcher/apitest-jsonpath"
)

...

func TestGetParams(t *testing.T) {
 cli := Client()
 apitest.New().
  EnableNetworking(&cli).
  Intercept(func(req *http.Request) {
   req.URL.RawQuery = "id=1&name=jack"
  }).
  Get("http://httpbin.org/get").
  Expect(t).
  Assert(
   jsonpath.Contains(`$.args.id`, "1")).
  Assert(
   jsonpath.Equal(`$.args.name`, "jack")).
  End()
}

req.URL.RawQuery: 用于定义get请求参数。

Assert() ,方法用于断言。

jsonpath 提供了断言方法,Contains判断包含,Equal判断相等。

  • post接口Form-data参数
...

func TestPostFormData(t *testing.T) {
 cli := Client()
 apitest.New().
  EnableNetworking(&cli).
  Post("http://httpbin.org/post").
  FormData("key1", "value1").
  FormData("key2", "value2").
  Expect(t).
  Assert(
   jsonpath.Chain().
    Equal(`$.form.key1`, "value1").
    Equal(`$.form.key2`, "value2").
    End()).
  End()
}

FormData() 用于设置form-Data格式的参数。

jsonpath 提供的断言同样支持链式调用。

  • post接口JSON参数
...

func TestPostJson(t *testing.T) {
 cli := Client()
 apitest.New().
  EnableNetworking(&cli).
  Post("http://httpbin.org/post").
  JSON(`{"message": "hi"}`).
  Expect(t).
  Assert(
   jsonpath.Chain().
    Contains(`$.data`, "message").
    Contains(`$.data`, "hi").
    End()).
  End()
}

JSON() 用于设置JSON()请求方法。

apitest 评价

官网:https://apitest.dev/

apitest 在完成http接口测试方面还是非常方便的,如果你被 go语言的 http 库蹂躏过一段时间之后感触更深;怎么说了,比如你经常被老板PUA,突然有一天老板居然当面表扬了你,大概就这种感觉。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK