go测试库之apitest
<h2 id="前言">前言</h2><p>使用go语言做开发差不多快一年了,主要用来写后端Web服务,从一开始吐槽他的结构体,比如创建个复杂的JSON格式数据,那是相当的痛苦。还有 err 处理写的巨麻烦。</p>
<p>当然,go 也有爽的地方,创建个线协程简直太简单了。</p>
<p>到后来慢慢接受,觉得效率还行,因为是静态强类型语言,在修改完项目代码之后,反而很有信心(如果出现低级的类型错误,直接编译出错了),相比 Python 就要反复检查两边,对修改的代码总时心里发虚。</p>
<p>go语言测试相关的东西都不咋地,比如自带的测试框架相比较 pytest 那是相当的简陋。今年开始给后端写单元测试(其实应该叫接口测试),发现 apitest库 眼前一亮。采用链式调用,和 HttpRunner 3.x 的链式调用颇有几分相似。</p>
<ul>
<li>HttpRunner 3.x</li>
</ul>
<pre><code class="language-python"># 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()
</code></pre>
<h2 id="apitest-测试库">apitest 测试库</h2>
<p>一个简单且可扩展的行为测试库。</p>
<p>测试库: https://github.com/steinfletcher/apitest jsonpath库: github.com/steinfletcher/apitest-jsonpath</p>
<ul>
<li>简单的get接口</li>
</ul>
<pre><code class="language-go">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()
}
</code></pre>
<p><code>New()</code>: 创建一个新的API测试。</p>
<p><code>EnableNetworking()</code>: EnableNetworking为提供的客户端启用网络,需要一个 http.Clinet。</p>
<p><code>Get()</code>: 发送get 请求,需要一个URL。</p>
<p><code>Expect()</code>: Expect将请求规范标记为完整。</p>
<p><code>Status()</code>: 断言http状态。http.StatusOK = 200</p>
<p><code>End()</code>: End运行测试,将结果返回给调用者。</p>
<ul>
<li>get接口带参数</li>
</ul>
<pre><code class="language-go">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()
}
</code></pre>
<p><code>req.URL.RawQuery</code>: 用于定义get请求参数。</p>
<p><code>Assert()</code> ,方法用于断言。</p>
<p>jsonpath 提供了断言方法,Contains判断包含,Equal判断相等。</p>
<ul>
<li>post接口Form-data参数</li>
</ul>
<pre><code>...
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()
}
</code></pre>
<p><code>FormData()</code> 用于设置form-Data格式的参数。</p>
<p>jsonpath 提供的断言同样支持链式调用。</p>
<ul>
<li>post接口JSON参数</li>
</ul>
<pre><code class="language-go">...
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()
}
</code></pre>
<p><code>JSON()</code> 用于设置JSON()请求方法。</p>
<h2 id="apitest-评价">apitest 评价</h2>
<p>官网:https://apitest.dev/</p>
<p>apitest 在完成http接口测试方面还是非常方便的,如果你被 go语言的 http 库蹂躏过一段时间之后感触更深;怎么说了,比如你经常被老板PUA,突然有一天老板居然当面表扬了你,大概就这种感觉。</p><br><br>
来源:https://www.cnblogs.com/fnng/p/17375690.html
頁:
[1]