httprunner基础

是什么?

面向http(s)协议的通用测试框架。

只需维护一份yaml脚本,即可实现自动化测试、性能测试、线上监控、持续集成等多种测试需求。

约定大于配置。

一站式解决方案,不限于http协议。

为什么?


httprunner3.0新特性

- 概念更简单、功能更易用、生态更强大

- pydantic类型系统

替换jsonschema,做类型的校验

思想来源于golang的struct

- 生成pytest用例

方便定位问题,便于单步调试

jinjia2模版生成

black格式化为标准pep8

- 集成pytest

allure

fixture

- 内置http web server

fastapi

更加易于进行二次开发、平台化

- 测试用例编写语法提示(todo)

httprunner核心概念

测试用例:参考维基百科

测试用例组织结构:用例分层,比如登陆是很常用的用例,把登陆用例分离,其他用例引用。

用例之间应该是没有依赖的。

- 去掉api、testsuite

- 测试用例的格式

config

teststeps

项目地址


用户文档:​​https://docs.httprunner.org/quickstart/​


环境准备:

建议使用虚拟环境。

1.搭建虚拟环境:

windows系统,新建一个目录,打开cmd,进入到新建的目录中,执行:python -m venv .

或者之间在cmd执行:python -m venv test_httprunner,会在所在目录新建一个目录

httprunner_python


 执行后,可以看到文件夹:

httprunner_sed_02

2.激活虚拟环境:

进入Scripts目录,执行:activate


3.安装httprunner,执行:pip3 install httprunner

注意:返回上一层目录再执行:pip3 install httprunner,最好不要在Scripts目录执行。

如果已经安装过httprunner,升级即可,执行:pip3 install -U HttpRunner。

 httprunner_用例_03


执行报错: 

/s eta 0:00:26ERROR: Exception:
Traceback (most recent call last):
File "e:\httprunner_ven\lib\site-packages\pip\_vendor\urllib3\response.py", li
ne 397, in _error_catcher
yield
File "e:\httprunner_ven\lib\site-packages\pip\_vendor\urllib3\response.py", li
ne 479, in read
data = self._fp.read(amt)
File "e:\httprunner_ven\lib\site-packages\pip\_vendor\cachecontrol\filewrapper
.py", line 62, in read
data = self.__fp.read(amt)
File "D:\python\lib\http\client.py", line 454, in read
n = self.readinto(b)
File "D:\python\lib\http\client.py", line 498, in readinto
n = self.fp.readinto(b)
File "D:\python\lib\socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "D:\python\lib\ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "D:\python\lib\ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "e:\httprunner_ven\lib\site-packages\pip\_internal\cli\base_command.py",
line 188, in main
status = self.run(options, args)
File "e:\httprunner_ven\lib\site-packages\pip\_internal\commands\install.py",
line 345, in run
resolver.resolve(requirement_set)
File "e:\httprunner_ven\lib\site-packages\pip\_internal\legacy_resolve.py", li
ne 196, in resolve
self._resolve_one(requirement_set, req)
File "e:\httprunner_ven\lib\site-packages\pip\_internal\legacy_resolve.py", li
ne 359, in _resolve_one
abstract_dist = self._get_abstract_dist_for(req_to_install)
File "e:\httprunner_ven\lib\site-packages\pip\_internal\legacy_resolve.py", li
ne 305, in _get_abstract_dist_for
abstract_dist = self.preparer.prepare_linked_requirement(
File "e:\httprunner_ven\lib\site-packages\pip\_internal\operations\prepare.py"
, line 195, in prepare_linked_requirement
unpack_url(
File "e:\httprunner_ven\lib\site-packages\pip\_internal\download.py", line 105
8, in unpack_url
unpack_http_url(
File "e:\httprunner_ven\lib\site-packages\pip\_internal\download.py", line 920
, in unpack_http_url
from_path, content_type = _download_http_url(link,
File "e:\httprunner_ven\lib\site-packages\pip\_internal\download.py", line 115
2, in _download_http_url
_download_url(resp, link, content_file, hashes, progress_bar)
File "e:\httprunner_ven\lib\site-packages\pip\_internal\download.py", line 861
, in _download_url
hashes.check_against_chunks(downloaded_chunks)
File "e:\httprunner_ven\lib\site-packages\pip\_internal\utils\hashes.py", line
75, in check_against_chunks
for chunk in chunks:
File "e:\httprunner_ven\lib\site-packages\pip\_internal\download.py", line 829
, in written_chunks
for chunk in chunks:
File "e:\httprunner_ven\lib\site-packages\pip\_internal\utils\ui.py", line 156
, in iter
for x in it:
File "e:\httprunner_ven\lib\site-packages\pip\_internal\download.py", line 794
, in resp_read
for chunk in resp.raw.stream(
File "e:\httprunner_ven\lib\site-packages\pip\_vendor\urllib3\response.py", li
ne 531, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "e:\httprunner_ven\lib\site-packages\pip\_vendor\urllib3\response.py", li
ne 496, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "D:\python\lib\contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "e:\httprunner_ven\lib\site-packages\pip\_vendor\urllib3\response.py", li
ne 402, in _error_catcher
raise ReadTimeoutError(self._pool, None, 'Read timed out.')
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files
.pythonhosted.org', port=443): Read timed out.
WARNING: You are using pip version 19.2.3, however version 20.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' comm
and.

httprunner_用例_04

httprunner_python_05

执行:python -m pip install --upgrade pip

升级pip。参考:​​https://jingyan.baidu.com/article/154b4631342f1528ca8f41ee.html​

Pip是一个Python的包管理工具,实际上它也可以被看待为是一个包,Pip相当于Linux上的yum,对python的开发者来说相当方便。

我们再也无需去焦头烂额的寻找whl包,直接通过pip就可以在线安装(前提是有网络+pip版本合适的情况下)。

如果pip的版本太老,很多包都无法安装。

httprunner_用例_06

 看到Successfully字样即升级成功。

httprunner_用例_07

安装成功,查看httprunner的版本:hrun -V

httprunner_用例_08


 httprunner_用例_09


 httprunner_python_10


 httprunner_sed_11


 httprunner_sed_12


 httprunner_用例_13


 httprunner_python_14


 httprunner_用例_15




4.脚手架生成项目:httprunner startproject test_hru

 这个旧命令不能使用了:hrun --startproject test_httprunner

生成测试脚手架异常:

(httprunner_ven) E:\httprunner_ven>hrun --startproject test_httprunner
Traceback (most recent call last):
File "D:\python\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\python\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "E:\httprunner_ven\Scripts\hrun.exe\__main__.py", line 7, in <module>
File "e:\httprunner_ven\lib\site-packages\httprunner\cli.py", line 126, in mai
n_hrun_alias
main()
File "e:\httprunner_ven\lib\site-packages\httprunner\cli.py", line 100, in mai
n
main_run(extra_args)
File "e:\httprunner_ven\lib\site-packages\httprunner\cli.py", line 34, in main
_run
raise exceptions.ParamsError("Missed testcase path")
httprunner.exceptions.ParamsError: Missed testcase path


执行:httprunner startproject test_hru

httprunner_python_16



5.使用pycharm打开项目

httprunner_sed_17


 httprunner_python_18


 httprunner_python_19


 注意:是打开test_hrun,而不是hrun-ven

 httprunner_python_20



httprunner_用例_21


 打开后的目录如下:httprunner_python_22



httprunner快速上手

运行demo用例

httprunner.exceptions.ParamsError: Missed testcase path

httprunner_python_23


(httprunner_ven) E:\httprunner_ven\test_hru>hrun testcases/demo_testcase.request.yml
Traceback (most recent call last):
File "D:\python\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\python\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "E:\httprunner_ven\Scripts\hrun.exe\__main__.py", line 7, in <module>
File "e:\httprunner_ven\lib\site-packages\httprunner\cli.py", line 126, in main_hrun_alias
main()
File "e:\httprunner_ven\lib\site-packages\httprunner\cli.py", line 100, in main
main_run(extra_args)
File "e:\httprunner_ven\lib\site-packages\httprunner\cli.py", line 34, in main_run
raise exceptions.ParamsError("Missed testcase path")
httprunner.exceptions.ParamsError: Missed testcase path

进入testcases目录,执行用例 

(httprunner_ven) E:\httprunner_ven\test_hru>cd testcases

(httprunner_ven) E:\httprunner_ven\test_hru\testcases>hrun demo_testcase_request.yml

执行用例报错:httprunner.exceptions.FunctionNotFound: sum_two is not found.

>       raise exceptions.FunctionNotFound(f"{function_name} is not found.")
E httprunner.exceptions.FunctionNotFound: sum_two is not found.

..\..\lib\site-packages\httprunner\parser.py:249: FunctionNotFound
================================================================================= short test summary info ==================================================================================
FAILED demo_testcase_request_test.py::TestCaseDemoTestcaseRequest::test_start - httprunner.exceptions.FunctionNotFound: sum_two is not found.


执行:  

(httprunner_ven) E:\httprunner_ven\test_hru\testcases>hrun demo_testcase_ref.yml


报错:

>           raise exceptions.ParamsError(f"Invalid testcase path: {path}")
E httprunner.exceptions.ParamsError: Invalid testcase path: E:\httprunner_ven\test_hru\testcases\testcases/demo_testcase_request.yml

..\..\lib\site-packages\httprunner\runner.py:210: ParamsError
================================================================================= short test summary info ==================================================================================
FAILED demo_testcase_ref_test.py::TestCaseDemoTestcaseRef::test_start - httprunner.exceptions.ParamsError: Invalid testcase path: E:\httprunner_ven\test_hru\testcases\testcases/demo_test...

录制脚本

httprunner_python_24

 删除静态资源

httprunner_python_25


导出har文件

httprunner_sed_26


 httprunner_sed_27


 httprunner_用例_28



生成yaml用例

har2case login.har -2y

E:\hrun_ven\test_hrun\har>har2case login.har -2y


httprunner_sed_29


 执行用例

hrun login.yml

项目地址:https://github.com/debugtalk/v3-demo-mubu


C:\Users\huang>hrun
usage: hrun [-h] [-V] [--log-level LOG_LEVEL] [--log-file LOG_FILE]
[--dot-env-path DOT_ENV_PATH] [--report-template REPORT_TEMPLATE]
[--report-dir REPORT_DIR] [--report-file REPORT_FILE] [--failfast]
[--save-tests] [--startproject STARTPROJECT]
[--validate [VALIDATE [VALIDATE ...]]]
[--prettify [PRETTIFY [PRETTIFY ...]]]
[testcase_paths [testcase_paths ...]]

One-stop solution for HTTP(S) testing.

positional arguments:
testcase_paths testcase file path

optional arguments:
-h, --help show this help message and exit
-V, --version show version
--log-level LOG_LEVEL
Specify logging level, default is INFO.
--log-file LOG_FILE Write logs to specified file path.
--dot-env-path DOT_ENV_PATH
Specify .env file path, which is useful for keeping
sensitive data.
--report-template REPORT_TEMPLATE
specify report template path.
--report-dir REPORT_DIR
specify report save directory.
--report-file REPORT_FILE
specify report file path, this has higher priority
than specifying report dir.
--failfast Stop the test run on the first error or failure.
--save-tests Save loaded tests and parsed tests to JSON file.
--startproject STARTPROJECT
Specify new project name.
--validate [VALIDATE [VALIDATE ...]]
Validate JSON testcase format.
--prettify [PRETTIFY [PRETTIFY ...]]
Prettify JSON testcase format.