API Testing 是一个基于 YAML 文件的开源接口测试工具,同时支持运行在本地、服务端。
在选择工具时,可以从很多方面进行考量、对比,以下几点是该工具的特色或者优点:
作为一款用于测试场景的工具,atest 本身的单元测试覆盖率达 89%,单测代码与业务逻辑代码量平分秋色;另外,每次代码改动都需要通过代码扫描、单元测试等流水线。
身材小巧,整个工具大小为 18M,支持 Windows、Linux、macOS 平台。
只有简单的可执行二进制文件,不像部分工具会给你的操作系统安装莫名其妙的系统启动项目、系统服务等。
基于 YAML 文件,提交到 Git 仓库后,天生支持团队协作,无需注册额外账号。
同时提供简单、高级两种模式的返回值断言,还包括 JSON Schema 以及针对 Kubernetes 资源的校验判断。
支持性能测试。
直接在 VS Code 中直接触发执行单个或整个测试文件。下面是测试用例样例:
#!api-testing
# yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
name: Kubernetes
api: |
{{default "https://172.11.0.18:6443" (env "SERVER")}}
items:
- name: pods
request:
api: /api/v1/namespaces/kube-system/pods
header:
Authorization: Bearer {{env "K8S_TOKEN"}}
expect:
verify:
- data.kind == "PodList"
- pod("kube-system", "kube-ovn-cni-55bz9").Exist()
- k8s("pods", "kube-system", "kube-ovn-cni-55bz9").Exist()
- k8s("deployments", "kube-system", "coredns").Exist()
- k8s("deployments", "kube-system", "coredns").ExpectField(2, "spec", "replicas")
- k8s("deployments", "kube-system", "coredns").ExpectField("kube-dns", "metadata", "labels", "k8s-app")
- k8s("daemonsets", "kube-system", "kube-ovn-cni").Exist()
- k8s({"kind":"virtualmachines","group":"kubevirt.io"}, "vm-test", "vm-win10-dkkhl").Exist()
- name: create-configmap
request:
api: /api/v1/namespaces/default/configmaps
header:
Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkRINXBRRi0zSURrbkRDWGhfVHpEaGFuOVdpcEVLSmFwYUI4Y1V5YjFpcUEifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJjbHVzdGVyLWFkbWluLXRva2VuLWtobnI0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImNsdXN0ZXItYWRtaW4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJmZmNlODg0Ny0yZGY4LTQyMTktOGRjYS1mNGRlMWYzNWNmYzkiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06Y2x1c3Rlci1hZG1pbiJ9.YapUNL7aSlAzlZwDqcMF1-eNpaEs0ZPwybV1uM289fDk8RwjHpLQzVZV0IewaOCAjifwyTyqs1Vgd4nF9I7CYPv64cjMcVTQHCj_-pAxXjiYEM9LkR_b__WGsd-3Z0aRrdyO4WS7moRxZ4kz7ULd_OtlHpq-cFIQtytOaQSZNSbxpa5uP7g7y-uv0nwXBSwqZL9j5XimGlYyy999Q8Vc2GLDrDdVp69wuvToODQzJV44nfuA_dhUFQOzC4sE7Dkq7JarrvZspstqLo1ULzt_Z-cZ-qAu_pUaLHkoLZH5o97g4UF8AXeFYLj8YP_IBP9uhDrm829pNHU82N6Hn-80NQ
method: POST
body: |
{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "config",
"namespace": "default",
"labels": {
"key": "{{randomKubernetesName}}"
}
},
"data": {
"key": "value"
}
}
expect:
statusCode: 201
- name: update-configmap
request:
api: /api/v1/namespaces/default/configmaps/config
header:
Authorization: Bearer {{env "K8S_TOKEN"}}
method: PUT
body: |
{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "config",
"namespace": "default"
},
"data": {
"key": "new value"
}
}
- name: get-configmap
request:
api: /api/v1/namespaces/default/configmaps/config
header:
Authorization: Bearer {{env "K8S_TOKEN"}}
method: PUT
body: |
{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "config",
"namespace": "default"
},
"data": {
"key": "new value"
}
}
expect:
bodyFieldsExpect:
"data/key": "new value"
- name: delete-configmap
request:
api: /api/v1/namespaces/default/configmaps/config
header:
Authorization: Bearer {{env "K8S_TOKEN"}}
method: DELETE