httprunner 2.x学习16 - 调用HttpRunner类执行用例

前言

httprunner 2.x版本底层用的是 unittest 框架运行的用例,执行用例的时候除了可以在命令行输入hrun运行用例,也可以调用HttpRunner类执行用例。

环境版本:httprunner==2.5.7

HttpRunner类介绍

在api.py找到HttpRunner类

class HttpRunner(object):
""" Developer Interface: Main Interface
Usage:

from httprunner.api import HttpRunner
runner = HttpRunner(
failfast=True,
save_tests=True,
log_level="INFO",
log_file="test.log"
)
summary = runner.run(path_or_tests)

"""

def __init__(self, failfast=False, save_tests=False, log_level="WARNING", log_file=None):
""" initialize HttpRunner.

Args:
failfast (bool): stop the test run on the first error or failure.
save_tests (bool): save loaded/parsed tests to JSON file.
log_level (str): logging level.
log_file (str): log file path.

"""

调用方法在上面已经有示例

from httprunner.api import HttpRunner
runner = HttpRunner(
failfast=True,
save_tests=True,
log_level="INFO",
log_file="test.log"
)
summary = runner.run(path_or_tests)

实例化HttpRunner类的时候,需要的四个参数

  • failfast 默认为False,作用是在遇到执行用例遇到第一个失败/异常停止运行

  • save_tests 默认为False,作用是讲运行的用例和结果保存到logs目录,保存为json文件

  • log_level 默认”WARNING”,设置运行日志的级别

  • log_file 默认None,保存日志文件路径

run运行用例

运行用例调用run方法

def run(self, path_or_tests, dot_env_path=None, mapping=None):
""" main interface.

Args:
path_or_tests:
str: testcase/testsuite file/foler path
dict: valid testcase/testsuite data
dot_env_path (str): specified .env file path.
mapping (dict): if mapping is specified, it will override variables in config block.

Returns:
dict: result summary

"""

run 方法有三个参数:

  • path_or_tests : 指定要运行的测试用例,支持传入两类参数:1.YAML/JSON 格式测试用例文件路径, 2.标准的测试用例结构体

  • dot_env_path: 指定加载环境变量文件(.env)的路径,默认为项目根目录.env 文件

  • mapping:

    变量映射,可用于对传入测试用例中的变量进行覆盖替换。

示例1,在hrun2_xuexi目录写一个run_case.py执行login目录的2个yaml用例

# run_case.py
from httprunner.api import HttpRunner

runner = HttpRunner(log_level="INFO")
runner.run('login')

设置log_level为INFO级别,运行后打印

INFO HttpRunner version: 2.5.7
INFO Start to run testcase: logincase
login case1
INFO POST http://127.0.0.1:8000/api/v1/login
.INFO
status_code: 200, response_time(ms): 239.38 ms, response_length: 109 bytes
----------------------------------------------------------------------

Ran 1 test in 0.241s
INFO
OK
Start to run testcase: logincase
login case1
INFO POST http://127.0.0.1:8000/api/v1/login
.INFO status_code: 200, response_time(ms): 225.15 ms, response_length: 109 bytes

----------------------------------------------------------------------
Ran 1 test in 0.225s

OK

save_tests 参数是保存运行的用例和运行结果

from httprunner.api import HttpRunner

runner = HttpRunner(
save_tests=True,
log_level="INFO",
)
runner.run('login')

运行后会生成一些json文件

log_file指定运行日志的目录,如果直接写一个日志名称’yoyo.txt’会出现找不到目录的报错

from httprunner.api import HttpRunner

runner = HttpRunner(failfast=True,
save_tests=True,
log_level="INFO",
log_file="yoyo.log")
runner.run('login')

运行报错:FileNotFoundError: [WinError 3] 系统找不到指定的路径。: '’

log_file参数需指定文件夹路径,如:logs/yoyo.log

from httprunner.api import HttpRunner

runner = HttpRunner(failfast=True,
save_tests=True,
log_level="INFO",
log_file="logs/yoyo.log")
runner.run('login')

会在logs目录生成yoyo.log的日志文件

failfast参数是在第一个用例运行失败的时候就停止运行

运行用例指定项目路径 debugtalk.py和.env

run运行用例,可以指定单个yaml或json文件,也可以指定运行一个文件夹内所有的用例

# 运行单个yaml
runner.run('login/login1.yml')
# 运行整个文件夹
runner.run('login')

HttpRunner 会自动以指定测试用例文件路径为起点,向上搜索 debugtalk.py 文件,并将 debugtalk.py 文件所在的文件目录作为当前工作目录。
同时自动搜索当前工作目录下搜索 .env 文件。这2个文件一般放到项目的根目录

dot_env_path 指定.env文件路径

runner.run('login', dot_env_path='.env')

mapping 传入测试用例中的变量进行覆盖替换

# mapping
override_mapping = {
"device_sn": "XXX"
}
runner.run("login", mapping=override_mapping)

path_or_tests 除了可以支持传入的yaml/json格式的文件,也可以支持dict格式的用例,如下

from httprunner.api import HttpRunner
dict_case = {
"testcases":[
{
"config":{
"name":"logincase1",
"base_url":"http://127.0.0.1:8000",
"variables":{}
},
"teststeps":[
{
"name":"login case1",
"request":{
"url":"/api/v1/login",
"method":"POST",
"json":{
"username":"test",
"password":123456
}
},
"validate": [
{
"eq": ["status_code",200]
}
]
}
]
},
{
"config":{
"name":"logincase2",
"base_url":"http://127.0.0.1:8000",
"variables":{}
},
"teststeps":[
{
"name":"login case1",
"request":{
"url":"/api/v1/login",
"method":"POST",
"json":{
"username":"test",
"password": 123456
}
},
"validate":[
{
"eq": ["status_code", 200]
}
]
}
]
}
]
}

runner = HttpRunner(log_level="INFO")
runner.run(dict_case)

可以自己写成符合上面用例格式的字典,也可以用run执行

《HttpRunner 3.x接口自动化实战》网易云视频课程

HttpRunner2.X 版本和 3.X 版本的区别到底有哪些?(吐血总结!)

(0)

相关推荐