Httprunner  实战

 AI开发测试实战 今天

 

图片

Python等安装

 

  1. 下载https://www.python.org/ Python 安装比较简单,直接解压,配置一下环境就好。

  2. 安装pycharm 

    下载地址为:https://www.jetbrains.com/pycharm/download/

     

    图片

 

建立一个环境python 环境

 

打开pycharm的,new建立一个new python project ,输入项目文件名称输入httprunnerdemo,然后创建,本机环境有多个python版本,这里选择用到python3.8版本

图片

 

 

确定python版本

  •  
  •  
 python --versionPython 3.8


 

安装httprunner

 

  •  
pip install httprunner

安装很多包

 

  •  
Installing collected packages: ...

 

用Charles|Fidder录制

 

用charles录制请求,另外保存为qa.har文件

har文件保存的是

  •  
  •  
{"log":{"version":"1.2","creator":{"name":"Charles Proxy","version":"4.5.6"},文件内容

用har2cast qa.har 命令会自动转成文件为qa_test.py

文件内容如下;

  •  
  •  
class TestLogin(HttpRunner):
    config = Config("testcase login").verify(False)
    teststeps = [        Step(            RunRequest("/user/login")                .put("https://mobile-api.toutiao-qa.com/user/login")                .with_headers(                **{                    "accept": "application/json",                    "x-hmac": "Ncj3GDIYau6cpLMq4Ptt3kSz/R1vTXrSl1wImxjSKtQ=",                    "x-client-id": "abcdefjhijklimnopqrstuvwxy001",                    "x-timestamp": "%s" % now_time,                    "x-session-token": "00077671-0424-488f-b063-35e20cdee840",                    "deviceid": "4a61bcb5-6568-4e44-80d8-73dd1c24b830",                    "content-type": "application/json",                    "content-length": "75",                    "accept-encoding": "gzip",                    "user-agent": "okhttp/3.14.4",                    "anonymous": "true",                }            )                .with_json(                {                    "email": "autotest001@toutiao.com",                    "password": "1234556",                    "type": "NORMAL",                }            )                .validate()                .assert_equal("status_code", 200)                .assert_equal("body.email", "autotest001@toutiao.com")                .assert_equal("body.first_name", "Autotest001")                .assert_equal("body.last_name", "auto_testing")                .assert_equal("body.phone", "13000900001")                .assert_equal("body.user_id", "43a67719-8a19-4331-8d0c-66a2ac8636b1")        ),    ]
if __name__ == "__main__":    TestLogin.test_start()
  1. 继承了HttpRunner,TestLogin

  2. teststeps里面就是一个一个Step 测试步骤,里面是一个一个数组内容

  3. 7-38行是一个RunRequest的请求,里面是put请求到/user/login 地址

  4. 9-21行是请求带header头,里面包含请求的设备名称和sessiontoken,这个会根据具体的header请求进行相应的修改

  5. 用with_json 请求json body内容

  6. 31-37就是测试校验部分,如果有时间可以进行相应修改。

 

或者用

har2case xxx/xxx/xxx.har -2y  /生成yml格式文件

  •  
  •  
har2case qa.har -2y2021-03-09 22:00:07.834 | INFO     | httprunner.ext.har2case.core:gen_testcase:356 - Start to generate testcase from /Users/XXX/PythonProjects/httprunnerdemo/qa.har2021-03-09 22:00:07.834 | INFO     | httprunner.ext.har2case.core:_make_testcase:347 - Extract info from HAR file and prepare for testcase.2021-03-09 22:00:07.837 | INFO     | httprunner.ext.har2case.utils:dump_yaml:108 - dump testcase to YAML format.2021-03-09 22:00:07.876 | INFO     | httprunner.ext.har2case.utils:dump_yaml:115 - Generate YAML testcase successfully: /Users/XXX/PythonProjects/httprunnerdemo/qa.yml2021-03-09 22:00:07.876 | INFO     | httprunner.ext.har2case.core:gen_testcase:377 - generated testcase: /Users/XXX/PythonProjects/httprunnerdemo/qa.ymlSentry is attempting to send 0 pending error messagesWaiting up to 2 secondsPress Ctrl-C to quit

会生成qa.yaml的json文件

​​​​​​​

  •  
config:    name: testcase description    variables: {}    verify: falseteststeps:-   name: /user/login    request:    ....

 

运行测试

1.运行方法1 

python qa_test.py    

​​​​​​​

  •  
python qa_test.py16152975526432021-03-09 21:45:52.716 | INFO     | httprunner.runner:test_start:450 - Start to run testcase: testcase description, TestCase ID: d7e1cad5-79e7-4a08-92fd-efa15647688e2021-03-09 21:45:52.717 | INFO     | httprunner.runner:__run_step:292 - run step begin: /user/login >>>>>>2021-03-09 21:45:52.970 | WARNING  | httprunner.client:request:188 - failed to get client address info: 'NoneType' object has no attribute 'connection'2021-03-09 21:45:52.971 | WARNING  | httprunner.client:request:196 - failed to get server address info: 'NoneType' object has no attribute 'connection'2021-03-09 21:45:52.971 | DEBUG    | httprunner.client:log_print:40 - ================== request details ==================method   : PUTurl      : https://mobile-api.toutiao-qa.com/user/loginheaders  : {    "Content-Length": "0"}cookies  : {}body     : None
2021-03-09 21:45:52.974 | DEBUG    | httprunner.client:log_print:40 - ================== response details ==================status_code : 0headers  : {}cookies  : {}encoding : Nonecontent_type : body     : 
2021-03-09 21:45:52.974 | ERROR    | httprunner.client:request:215 - HTTPSConnectionPool(host='mobile-api.toutiao-qa.com', port=443): Max retries exceeded with url: /user/login (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x10c1ebaf0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))2021-03-09 21:45:52.975 | ERROR    | httprunner.response:validate:263 - assert status_code equal 200(int)       ==> failcheck_item: status_codecheck_value: 0(int)assert_method: equalexpect_value: 200(int)2021-03-09 21:45:52.976 | ERROR    | httprunner.response:validate:263 - assert body.email equal autotest001@toutiao.com(str)    ==> failcheck_item: body.emailcheck_value: None(NoneType)assert_method: equalexpect_value: autotest001@toutiao.com(str)2021-03-09 21:45:52.976 | ERROR    | httprunner.response:validate:263 - assert body.first_name equal Autotest001(str)   ==> failcheck_item: body.first_namecheck_value: None(NoneType)assert_method: equalexpect_value: Autotest001(str)2021-03-09 21:45:52.977 | ERROR    | httprunner.response:validate:263 - assert body.last_name equal auto_testing(str)   ==> failcheck_item: body.last_namecheck_value: None(NoneType)assert_method: equalexpect_value: auto_testing(str)2021-03-09 21:45:52.978 | ERROR    | httprunner.response:validate:263 - assert body.phone equal 2009000001(str) ==> failcheck_item: body.phonecheck_value: None(NoneType)assert_method: equalexpect_value: 2009000001(str)2021-03-09 21:45:52.978 | ERROR    | httprunner.response:validate:263 - assert body.user_id equal 43a67719-8a19-4331-8d0c-66a2ac8636b1(str)     ==> failcheck_item: body.user_idcheck_value: None(NoneType)assert_method: equalexpect_value: 43a67719-8a19-4331-8d0c-66a2ac8636b1(str)2021-03-09 21:45:52.979 | ERROR    | httprunner.runner:log_req_resp_details:194 - ******************************** DETAILED REQUEST & RESPONSE ********************************====== request details ======url: https://mobile-api.toutiao-qa.com/user/loginmethod: PUTheaders: {'accept': 'application/json', 'x-hmac': 'Ncj3GDIYau6cpLMq4Ptt3kSz/R1vTXrSl1wImxjSKtQ=', 'x-client-id': 'abcdefjhijklimnopqrstuvwxy001', 'x-timestamp': '1615297552643', 'x-session-token': '00077671-0424-488f-b063-35e20cdee840', 'deviceid': '4a61bcb5-6568-4e44-80d8-73dd1c24b830', 'content-type': 'application/json', 'content-length': '75', 'accept-encoding': 'gzip', 'user-agent': 'okhttp/3.14.4', 'x-anonymous': 'true', 'HRUN-Request-ID': 'HRUN-d7e1cad5-79e7-4a08-92fd-efa15647688e-552718'}params: {}data: Nonecookies: {}timeout: 120allow_redirects: Trueverify: Falsejson: {'email': 'autotest001@toutiao.com', 'password': 'pwd11111', 'type': 'NORMAL'}
====== response details ======status_code: 0headers: {}body: ''
2021-03-09 21:45:52.979 | INFO     | httprunner.runner:test_start:460 - generate testcase log: /Users/apple/PythonProjects/httprunnerdemo/logs/d7e1cad5-79e7-4a08-92fd-efa15647688e.run.log

check_value: None(NoneType)assert_method: equalexpect_value: 43a67719-8a19-4331-8d0c-66a2ac8636b1(str)

请求的地址需要特别的请求,不是每台服务都可以对外允许请求。请求很多都是失败的,没有关系,进行调试。

 

2.运行方法2 

 hrun qa.yaml

​​​​​​​​​​​​​​

  •  
qa_test.py F                                                                                                                                                         [100%]
================================================================================= FAILURES =================================================================================__________________________________________________________________________ TestCaseQa.test_start ___________________________________________________________________________v

安装allure-pytest

 

  •  
pip install allure-pytest

Successfully installed allure-pytest-2.8.36 allure-python-commons-2.8.36 six-1.15.0,提示成功!

运行带allure的报告的命令

 

  •  
hrun --alluredir allure-results --clean-alluredir testcasesname

 

执行后可以看到多了一个alllure-results的目录

目录里面就是运行的pytest的结构集。

查看Allure report 报告

图片

总结

1.httprunner ,对于固定的json请求,比较方便,如果需要动态的json数据,需要二次进行开发json串,如时间请求,需要进行time 新的时间串,录制的脚本基本都是判断录制时候的时间串,需要二次修改判断。

2.本人进行的录制,生成qa_test.py,对python编辑,对文本yaml

文件编辑,如果请求和json很长,格式比较容易乱,个人认为用qa_test.py 比较方便,再就是建立allure的报告,allure也是通用的测试报告,结果和图表比较完备。

因个人水平有限,请多支持;谢谢!

 

 

--END--


图片 点击屏末  | 

 

专注于AI,开发、测试技术研究

图片

图片
关注  智慧rdda大成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值