前言
如何快速搭建一个httprunner项目呢?我们可以使用脚手架,脚手架就是自动地创建一些目录,形成一个项目的架构,不需要我们再手动的去创建
查看创建新项目的命令
先来查看一下帮助命令
httprunner startproject -h
(httprunner_env) ➜ hrun httprunner startproject -h usage: httprunner startproject [-h] [project_name] positional arguments: project_name Specify new project name. optional arguments: -h, --help show this help message and exit
可以看出,唯一需要指定的就是项目名称。
创建新项目
首先在用户目录创建一个hrun文件夹
,进入文件夹创建一个名为hrun_demo
的新项目
httprunner startproject hrun_demo
(httprunner_env) ➜ ~ mkdir hrun (httprunner_env) ➜ ~ cd hrun (httprunner_env) ➜ hrun httprunner startproject hrun_demo 2021-02-03 11:23:58.081 | INFO | httprunner.scaffold:create_scaffold:43 - Create new project: hrun_demo Project Root Dir: /Users/jkc/hrun/hrun_demo created folder: hrun_demo created folder: hrun_demo/har created folder: hrun_demo/testcases created folder: hrun_demo/reports created file: hrun_demo/testcases/demo_testcase_request.yml created file: hrun_demo/testcases/demo_testcase_ref.yml created file: hrun_demo/debugtalk.py created file: hrun_demo/.env created file: hrun_demo/.gitignore $ tree hrun_demo -a hrun_demo ├── .env ├── .gitignore ├── debugtalk.py ├── har ├── reports └── testcases ├── demo_testcase_ref.yml └── demo_testcase_request.yml 3 directories, 5 files Sentry is attempting to send 0 pending error messages Waiting up to 2 seconds Press Ctrl-C to quit
如果你指定的项目名已存在,会有如下警告:
(httprunner_env) ➜ hrun httprunner startproject hrun_demo 2021-02-03 11:27:16.028 | WARNING | httprunner.scaffold:create_scaffold:33 - Project folder hrun_demo exists, please specify a new project name. $ tree hrun_demo -a hrun_demo ├── .env ├── .gitignore ├── debugtalk.py ├── har ├── reports └── testcases ├── demo_testcase_ref.yml └── demo_testcase_request.yml 3 directories, 5 files Sentry is attempting to send 0 pending error messages Waiting up to 2 seconds Press Ctrl-C to quit
项目结构
创建完成项目后,查看项目结构有哪些目录?
- har:可以存放录制导出的.har文件
- reports:存储 HTML 测试报告
- testcases:存放测试用例
- .env:放置在项目根目录下,存放环境变量
- debugtalk.py:放置在项目根目录下(借鉴了pytest的conftest文件的设计),用于写一些公共函数和方法
运行用例
创建的新项目中,默认有2个yaml
文件形式的用例(demo_testcase_ref.yml
和demo_testcase_request.yml
),我们运行看看
hrun hrun_demo
运行结果:
(httprunner_env) ➜ hrun hrun hrun_demo 2021-02-03 13:33:58.336 | INFO | httprunner.make:__make:512 - make path: /Users/jkc/hrun/hrun_demo 2021-02-03 13:33:58.342 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-02-03 13:33:58.345 | INFO | httprunner.loader:load_dot_env_file:127 - Loading environment variables from /Users/jkc/hrun/hrun_demo/.env 2021-02-03 13:33:58.345 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: USERNAME 2021-02-03 13:33:58.345 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: PASSWORD 2021-02-03 13:33:58.347 | INFO | httprunner.make:make_testcase:349 - start to make testcase: /Users/jkc/hrun/hrun_demo/testcases/demo_testcase_ref.yml 2021-02-03 13:33:58.364 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-02-03 13:33:58.365 | INFO | httprunner.make:make_testcase:349 - start to make testcase: /Users/jkc/hrun/hrun_demo/testcases/demo_testcase_request.yml 2021-02-03 13:33:58.365 | INFO | httprunner.make:make_testcase:442 - generated testcase: /Users/jkc/hrun/hrun_demo/testcases/demo_testcase_request_test.py 2021-02-03 13:33:58.366 | INFO | httprunner.make:make_testcase:442 - generated testcase: /Users/jkc/hrun/hrun_demo/testcases/demo_testcase_ref_test.py 2021-02-03 13:33:58.377 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-02-03 13:33:58.378 | INFO | httprunner.make:make_testcase:349 - start to make testcase: /Users/jkc/hrun/hrun_demo/testcases/demo_testcase_request.yml 2021-02-03 13:33:58.378 | INFO | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ... reformatted /Users/jkc/hrun/hrun_demo/testcases/demo_testcase_ref_test.py reformatted /Users/jkc/hrun/hrun_demo/testcases/demo_testcase_request_test.py All done! ✨ ???? ✨ 2 files reformatted. 2021-02-03 13:33:58.658 | 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.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 rootdir: /Users/jkc/hrun plugins: metadata-1.11.0, html-2.1.1 collected 2 items hrun_demo/testcases/demo_testcase_request_test.py . [ 50%] hrun_demo/testcases/demo_testcase_ref_test.py . [100%] ================================================================================================== 2 passed in 4.87s ===================================================================================================
我们再次查看项目结果会发现根目录下多出了logs日志文件夹
,testcases中多出了2个py文件
这里应该是作者源码中运行后写的代码结果了