HttpRunner v3.x支持三种测试用例格式pytest,YAML和JSON。强烈建议以pytest格式而不是以前的YAML/JSON格式编写和维护测试用例(官方推荐的)
相互转换格式关系图如下:
配置安装
(mac下httprunner3 安装以及可能遇到的问题参考上篇文章)
主要用到4 个命令:
httprunner:主命令,用于所有命令
hrun :httprunner run别名,用于运行YAML / JSON / pytest测试用例
进入指定目录创建项目
hmake:httprunner make别名,用于将YAML / JSON测试用例转换为pytest
har2case:httprunner har2case别名,用于将HAR转换为YAML / JSON测试用例
我电脑里har2case需要单独安装一下,遇到命令不好使时解决方案和 hrun 一样
pip3 install har2case
创建项目
进入指定目录 me8,创建 httprunner 项目
httprunner startproject httprunner3_test
创建完成后在 me8 下自动创建一个 httprunner3_test 文件夹
创建用例
1.脚本录制方式
第一步 录制脚本
使用 Charles或其他工具录制脚本,并将待测接口保存为.har 文件(右键 export),存入项目/har下
第二步 .har转换成 .yml/.json
使用 har2case 转换成.yml/.json 用例
user:pythonAutoTest ss$ cd me8/httprunner3_test/har/
user:har ss$ har2case login.har
INFO:root:Start to generate testcase.
INFO:root:dump testcase to JSON format.
INFO:root:Generate JSON testcase successfully: login.json
user:har ss$ har2case login.har -2y
INFO:root:Start to generate testcase.
INFO:root:dump testcase to YAML format.
INFO:root:Generate YAML testcase successfully: login.yml
user:har ss$
在 har 下生成两个文件 login.json/login.yml
第三步 运行脚本
方式 1:运行单个脚本
进入 har 下,执行 hrun login.yml
方式 2运行整个项目脚本
进入me8/下,执行 hrun httprunner3_test
运行后会发现在 har 下自动生成一个 login_test.py 文件,因为 httprunner 是基于 pytest框架的,不管用例用什么维护,最终都转换pytest文件执行
一般情况下直接执行录制的脚本不会运行成功,因为录制的东西有些还需要手动改一下,所有有时候录制并不可靠
2.手动编写pytest测试用例
删掉 har 下的 yml/json文件,删掉项目自带 demo
直接用PyCharm写 pytest测试用例
1.在 testcases 下创建 python 文件 login_test.py
from httprunner import HttpRunner, Config, Step, RunRequest
class TestCaseLogin(HttpRunner):
config = Config('hrun3_demo').base_url('http://xxx:80').export(*['vars_name'])
teststeps = [
Step(
RunRequest('login')
.post('/xxx/login.html')
.with_headers(**{'X-Requested-With': 'XMLHttpRequest'})
.with_data({'accounts':'ss','pwd':'123456'})
.extract()
.with_jmespath('body.msg','value_msg')
.validate()
.assert_equal('body.msg','登录成功')
.assert_equal('body.code',0)
)
]
if __name__ == '__main__':
TestCaseLogin().test_start()
2.单独运行,进入 testcases 下执行 hrun login_test.py
user:httprunner3_test ss$ cd testcases/
user:testcases ss$ hrun login_test.py
2021-01-26 19:20:50.125 | INFO | httprunner.make:__make:512 - make path: /Users/ss/Documents/Python/pythonAutoTest/me8/httprunner3_test/testcases/login_test.py
2021-01-26 19:20:50.125 | INFO | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ...
No Path provided. Nothing to do 😴
2021-01-26 19:20:50.357 | INFO | httprunner.cli:main_run:56 - start to run tests with pytest. HttpRunner version: 3.1.4
==================================================================== test session starts =====================================================================
platform darwin -- Python 3.7.2, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: /Users/ss/Documents/Python/pythonAutoTest/me8/httprunner3_test/testcases
plugins: assume-2.4.2, allure-pytest-2.8.16, metadata-1.11.0, html-2.1.1, Faker-4.18.0
collected 1 item
login_test.py . [100%]
===================================================================== 1 passed in 0.38s ======================================================================
user:testcases ss$
3.运行项目,进入 me8下执行hrun
user:me8 ss$ hrun httprunner3_test/
2021-01-26 19:27:24.320 | INFO | httprunner.make:__make:512 - make path: /Users/ss/Documents/Python/pythonAutoTest/me8/httprunner3_test/
2021-01-26 19:27:24.323 | INFO | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ...
No Path provided. Nothing to do 😴
2021-01-26 19:27:24.563 | INFO | httprunner.cli:main_run:56 - start to run tests with pytest. HttpRunner version: 3.1.4
==================================================================== test session starts =====================================================================
platform darwin -- Python 3.7.2, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: /Users/sihaiyue/Documents/Python/pythonAutoTest/me8
plugins: assume-2.4.2, allure-pytest-2.8.16, metadata-1.11.0, html-2.1.1, Faker-4.18.0
collected 1 item
httprunner3_test/testcases/login_test.py . [100%]
===================================================================== 1 passed in 0.39s ======================================================================
user:me8 ss$
到此,一个简单的实例就完成了
后续更新 case 关联和参数化