AI开发测试实战 今天
Python等安装
-
下载https://www.python.org/ Python 安装比较简单,直接解压,配置一下环境就好。
-
安装pycharm
下载地址为:https://www.jetbrains.com/pycharm/download/
建立一个环境python 环境
打开pycharm的,new建立一个new python project ,输入项目文件名称输入httprunnerdemo,然后创建,本机环境有多个python版本,这里选择用到python3.8版本
确定python版本
python --version
Python 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()
-
继承了HttpRunner,TestLogin
-
teststeps里面就是一个一个Step 测试步骤,里面是一个一个数组内容
-
7-38行是一个RunRequest的请求,里面是put请求到/user/login 地址
-
9-21行是请求带header头,里面包含请求的设备名称和sessiontoken,这个会根据具体的header请求进行相应的修改
-
用with_json 请求json body内容
-
31-37就是测试校验部分,如果有时间可以进行相应修改。
或者用
har2case xxx/xxx/xxx.har -2y /生成yml格式文件
har2case qa.har -2y
2021-03-09 22:00:07.834 | INFO | httprunner.ext.har2case.core:gen_testcase:356 - Start to generate testcase from /Users/XXX/PythonProjects/httprunnerdemo/qa.har
2021-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.yml
2021-03-09 22:00:07.876 | INFO | httprunner.ext.har2case.core:gen_testcase:377 - generated testcase: /Users/XXX/PythonProjects/httprunnerdemo/qa.yml
Sentry is attempting to send 0 pending error messages
Waiting up to 2 seconds
Press Ctrl-C to quit
会生成qa.yaml的json文件
config:
name: testcase description
variables: {}
verify: false
teststeps:
- name: /user/login
request:
....
运行测试
1.运行方法1
python qa_test.py
python qa_test.py
1615297552643
2021-03-09 21:45:52.716 | INFO | httprunner.runner:test_start:450 - Start to run testcase: testcase description, TestCase ID: d7e1cad5-79e7-4a08-92fd-efa15647688e
2021-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 : PUT
url : https://mobile-api.toutiao-qa.com/user/login
headers : {
"Content-Length": "0"
}
cookies : {}
body : None
2021-03-09 21:45:52.974 | DEBUG | httprunner.client:log_print:40 -
================== response details ==================
status_code : 0
headers : {}
cookies : {}
encoding : None
content_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) ==> fail
check_item: status_code
check_value: 0(int)
assert_method: equal
expect_value: 200(int)
2021-03-09 21:45:52.976 | ERROR | httprunner.response:validate:263 - assert body.email equal autotest001@toutiao.com(str) ==> fail
check_item: body.email
check_value: None(NoneType)
assert_method: equal
expect_value: autotest001@toutiao.com(str)
2021-03-09 21:45:52.976 | ERROR | httprunner.response:validate:263 - assert body.first_name equal Autotest001(str) ==> fail
check_item: body.first_name
check_value: None(NoneType)
assert_method: equal
expect_value: Autotest001(str)
2021-03-09 21:45:52.977 | ERROR | httprunner.response:validate:263 - assert body.last_name equal auto_testing(str) ==> fail
check_item: body.last_name
check_value: None(NoneType)
assert_method: equal
expect_value: auto_testing(str)
2021-03-09 21:45:52.978 | ERROR | httprunner.response:validate:263 - assert body.phone equal 2009000001(str) ==> fail
check_item: body.phone
check_value: None(NoneType)
assert_method: equal
expect_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) ==> fail
check_item: body.user_id
check_value: None(NoneType)
assert_method: equal
expect_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/login
method: PUT
headers: {'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: None
cookies: {}
timeout: 120
allow_redirects: True
verify: False
json: {'email': 'autotest001@toutiao.com', 'password': 'pwd11111', 'type': 'NORMAL'}
====== response details ======
status_code: 0
headers: {}
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: equal
expect_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大成