本實例中通過獲取登錄接口返回的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、查看生成的測試報告:

