一、前言
前面讲的比较理论,本篇主要用实际项目,体现下HttpRunner的一些基本用法。
二、项目场景实例说明
1、业务流程:登录——创建版单——领取版单
2、接口信息如下:
- 登录:/auth/login_by_password
- 创建版单:type/add
- 领取版单:type/received
3、接口依赖说明:
- 创建版单的前提是要先登录系统
- 领取版单需要获取前一个接口步骤返回的id
三、测试用例代码
1、登录接口用例
# NOTE: Generated By HttpRunner v3.1.5 # FROM: har\login.har from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase class TestCaseLogin(HttpRunner): config = Config("登录") .verify(False) .base_url("https://XXX.com")\ #在config定义url后,teststeps里就直接写接口路径就行了 .variables( **{"username":"李白","psw":"123456"} #在config定义变量,step里都可以用 ) .export(*["userId","userNo","userName"]) #下面step提取参数设为变量后,在config里导出,好处是其他步骤跟用例都可以引用 teststeps = [ Step( RunRequest("登录接口") .post("/auth/login_by_password") .with_headers( **{ "accept": "application/json, text/plain, */*", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "content-type": "application/json;charset=UTF-8", "accept-language": "zh-CN,zh;q=0.9", } ) .with_json( { "employeeName": "$username", #引用config定义的变量 "loginType": "PASSWORD", "password": "$psw", #引用config定义的变量 } ) .extract() #提取返回参数 .with_jmespath("body.data.userId","userId") .with_jmespath("body.data.userNo","userNo") .with_jmespath("body.data.userName","userName") .validate() #断言 .assert_equal("status_code", 200) .assert_equal("body.successful", True) .assert_equal("body.code", "200") .assert_equal("body.message", "请求成功") ), ] if __name__ == "__main__": TestCaseLogin().test_start()
2、业务流程用例
# NOTE: Generated By HttpRunner v3.1.5 # FROM: har\received.har from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from testcases.login_test import TestCaseLogin #因为要引用登录用例,所以要先import class TestCaseReceived(HttpRunner): config = Config("业务主流程") .verify(False) .base_url("https://XXX.cn") .export(*["prototypeId"]) teststeps = [ Step(
#调用另一个用例时,注意是用RunTestCase("自己随意命名").call(另一个用例的类名) RunTestCase("调用登录用例").call(TestCaseLogin).export(*["userId","userNo","userName"]) ), Step( RunRequest("创建版单接口") .post("/XXX/type/add") .with_headers( **{ "OSChannel": "web", "Accept": "application/json, text/plain, */*", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "CURRENT-USER-ID": "100", "Content-Type": "application/json;charset=UTF-8", "CURRENT-USER-NAME": "%E6%E5%B9%B3", "Accept-Language": "zh-CN,zh;q=0.9", } ) .with_json( { "pictureList": [ { "url": "https://794802905c9a3699256.png", "pictureTypeEnum": "POSITIVE", }, ], "purchaserInfo": { "bdId": "100", }, "purchaserSource": "", "saleGroup": "国内-全国", "deliveryTime": "2021-07-16 00:00:00", "category": "男装-上装-衬衫-", "referType": "IMG", "referRemark": "同款复版", ) .extract() #提取返回参数,下面的领取接口需要用到这个id,注意要在config里export .with_jmespath("body.data.prototypeId","prototypeId") .validate() .assert_equal("status_code", 200) .assert_equal(‘headers."Content-Type"‘, "application/json") .assert_equal("body.successful", True) .assert_equal("body.code", "200") .assert_equal("body.message", "请求成功") ), Step( RunRequest("领取接口") .put("/XXXtype/received") .with_headers( **{ "Accept": "application/json, text/plain, */*", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "CURRENT-USER-ID": "100", "Content-Type": "application/json;charset=UTF-8", "CURRENT-USER-NAME": "%EB7%E5%B9%B3", "Accept-Language": "zh-CN,zh;q=0.9", } ) .with_json( { "prototypeId": "$prototypeId", #直接$引用变量就行了 "currentUserId": "1078", "workerId": "1078", "currentUserName": "李白", "currentUserCode": "00878", } ) .validate() .assert_equal("status_code", 200) .assert_equal(‘headers."Content-Type"‘, "application/json") .assert_equal("body.successful", True) .assert_equal("body.code", "200") .assert_equal("body.message", "请求成功") .assert_equal("body.data", None) ), ] if __name__ == "__main__": TestCaseReceived().test_start()