本实例中通过获取登录接口返回的token,将token值传到获取项目列表的请求头中,实现关联参数的应用:
1、.env中定义BASE_URL、USERNAME、PASSWORD三个变量值:
BASE_URL=http://127.0.0.1:8000 USERNAME=benben PASSWORD=123456
2、在api目录下新建login_api.yml文件,用于正常登录:
name: 正常登录 base_url: ${ENV(BASE_URL)} variables: username: ${ENV(USERNAME)} password: ${ENV(PASSWORD)} status: 200 request: url: /user/login/ method: POST headers: Content-Type: "application/json" json: username: $username password: $password validate: - eq: ["status_code", 200]
3、在api目录下新建projects_list.yml文件,用于获取项目列表:
name: 项目列表 base_url: ${ENV(base_url)} variables: token: $token p: $p s: $s title: $title status: $status request: url: /projects/?p=$p&s=$s method: GET headers: Content-Type: "application/json" Authorization: JWT $token validate: - eq: ["status_code", $status]
4、在testcases目录下新建projects_list_testcase.yml文件,用于获取登录接口的token:
config: name: 项目列表 variables: login_title: 登录 title: $title status: $status teststeps: - name: $login_title api: api/login_api.yml extract: - token: content.token validate: - eq: ["status_code", $status] - name: $title api: api/projects_list.yml validate: - eq: ["status_code", 200]
5、在testsuites目录下新建projects_list_suite.yml文件,用于实现参数化:
config: name: 项目列表 testcases: - name: $title testcase: testcases/projects_list_testcase.yml parameters: - title-p-s-status: - ["获取第一页数据", 1, 1, 200] - ["获取第二页数据", 2, 1, 200]
6、运行:
hrun testsuites/projects_list_testsuite.yml --report-template templates/ext_reports_template.html
7、查看生成的测试报告: