httprunner(3)用脚手架快速搭建项目[通俗易懂]

2022-09-16 10:43:18 浏览数 (1)

前言

如何快速搭建一个httprunner项目呢?我们可以使用脚手架,脚手架就是自动地创建一些目录,形成一个项目的架构,不需要我们再手动的去创建

查看创建新项目的命令

先来查看一下帮助命令

代码语言:javascript复制
httprunner startproject -h
代码语言:javascript复制
(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的新项目

代码语言:javascript复制
httprunner startproject hrun_demo
代码语言:javascript复制
(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

如果你指定的项目名已存在,会有如下警告:

代码语言:javascript复制
(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.ymldemo_testcase_request.yml),我们运行看看

代码语言:javascript复制
hrun hrun_demo

运行结果:

代码语言:javascript复制
(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! ✨ 


	

0 人点赞