# chamberlain-test **Repository Path**: coregiu/chamberlain-test ## Basic Information - **Project Name**: chamberlain-test - **Description**: Test of chamberlain - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-04-08 - **Last Updated**: 2021-05-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Chamberlain测试用例库 基于restbird:https://restbird.org/tutorial/tutorial.html 中文:https://cn.restbird.org/tutorial/tutorial.html restbird功能总结: ​ 1 支持带上下文的rest api测试 ​ 2 支持对请求进行前置定制处理,和对响应体定制自检 ​ 3 支持定制全局变量实现上下文管理,支持PYTHON和GO两种语言实现定制脚本 ​ 4 支持请求录制回放,以生成场景化用例集 ​ 5 支持MOCK SERVER,包括数据库MOCK ​ 6 支持任务编排,将测试活动编排为一个任务,包括发邮件等 ​ 7 支持灵活的用户管理,共享用例 ​ 8 用户复制复用方便,体验较好 注:当前restbird并未表明身份,license不明朗,GITHUB上只有使用DEMO,没有源码。 Get start: 1 下载docker镜像: docker pull restbird/rest 如果镜像找不到,执行 docker pull docker.mirrors.ustc.edu.cn/restbird/rest 2 在本地创建运行工作目录,如 d:/restbird。 如果用以前的工程,则在容器中挂载以前的工作目录,或把以前的工作目录拷到当前工作空间即可。 3 启动容器: docker run -d --name resetbird -p 8080:8080 -v /giu/testcase:/data/restbird restbird/rest:latest 4 打开浏览器输入 localhost:8080 打开portal 输入口令: admin/admin完成登录 5 建立自己的工作目录,并建立用例。 6 上下文通过全局参数实现,在API connections TAB页,环境变量设置中添加全局变量。在后续使用过程中,{{var_name}}来表示变量。 go语言全局变量读取和设置分别使用: ~~~go ctx.SetGlobal(key, value) ctx.GetGlobal(key) ~~~ PYTHON语言全局变量读取和设置分别使用: ~~~python ctx.SetGlobal(key, value) ctx.GetGlobal(key) ~~~ go语言局部变量读取和设置分别使用: ~~~go ctx.SetVars(key, value) ctx.GetVars(key) ~~~ PYTHON语言全局变量读取和设置分别使用: ~~~python ctx.SetVars(key, value) ctx.GetVars(key) ~~~ 7 在pre-request scripts, response-check scripts中分别做请求前置处理和响应结果定制检查(默认以http 200响应码为正确结果)。 8 例如当前用例中,设置全局变量host_ip和x_auth_token作为全局变量。因为不同环境请求IP随时会变,到时直接替换一个变量定义就好。x_auth_token是登录接口执行后的结果,同时作为后续接口请求的HEADER参数。 那么实现如下: (1)在登录接口的后置检查中定制如下脚本: ~~~go package api import "callapi" import "net/http" import "encoding/json" import "fmt" import "io/ioutil" type Token struct { TokenId string IssueTime string ExpireTime string Issuer string } func (c CallBack) ResponseValidate(resp *http.Response, ctx *callapi.RestBirdCtx) bool { body, _ := ioutil.ReadAll(resp.Body) var token Token err := json.Unmarshal(body, &token) fmt.Println("----------------" +token.TokenId) if err != nil { fmt.Printf(err.Error()) } ctx.SetVars("x_auth_token", token.TokenId) return token.TokenId != "" } ~~~ (2) 假设需要校验相同用户连续两次登录返回相同的token,那么紧接着写第二个用例,定制返回结果检查如下: ~~~go package api import "callapi" import "net/http" import "encoding/json" import "fmt" import "io/ioutil" type Token struct { TokenId string IssueTime string ExpireTime string Issuer string } func (c CallBack) ResponseValidate(resp *http.Response, ctx *callapi.RestBirdCtx) bool { body, _ := ioutil.ReadAll(resp.Body) var token Token err := json.Unmarshal(body, &token) fmt.Println("----------------" +token.TokenId) // 这次请求的TOKEN if err != nil { fmt.Printf(err.Error()) } return token.TokenId == ctx.GetVars("x_auth_token")//上次的token } ~~~ (3) 在后续业务使用中,在header中设置请求头 X-AUTH-TOKEN:{{x_auth_token}}