1.pytest参数及运行方式
pytest
#html报告
pytest-html
#多线程
pytest-xdist
#控制用例执行顺序
pytest-ordering
#失败用例重跑
pytest-rerunfailures
#基础路径
pytest-base-url
#生成美观的alllure报告
allure-pytest
[pytest]
#配置常用参数
addopts = -vs --html=./commens/r.html -n 4
#配制测试用例位置
testpaths = ./test_api
python_files = test_*.py
python_classes = Test*
python_functions = test_*
#基础路径
base_url = "http://192.163.0.01"
#创建标记
markers =
smoke:冒烟用例
users:用户模块
order:订单模块
运行参数:
-vs -v详细信息 -s调试信息
--html=./commens/r.html 生成html报告
-n 多线程运行
@pytest.mark.run(order= 1) 调整执行顺序order=1表示第一个执行
--reruns=2 失败重跑两次
--maxfail ==2 有两次用例失败就停止运行
-k 根据测试用例中关键字字符串选择运行
-m标记:
1.创建标记, 在pytest.ini中
#创建标记 markers = smoke:冒烟用例 users:用户模块 order:订单模块
2.给测试用例打标记
@pytest.mark.users def test_login(self): prinf("测试用例")
3.在pytest.ini中加入运行参数
#配置常用参数 addopts = -vs -m "users or order"
@pytest.mark.skip(reason="无条件跳过") 无条件跳过测试用例
@pytest.mark.skipif(age <= 30 , reason="有条件跳过") 有条件跳过测试用例
配置基础路径
在pytest.ini中加入base_url,就相当于创建了一个固件
#基础路径
base_url = "http://192.163.0.01"
2.前后置Fixture, 与代码耦合性更低
@pytest.fixture(scope="作用域",autouse="自动/手动")
scope的值(作用域):
function:函数
class: 类
module: 模块, py文件
session: 回话,所有用例
autouse是否自动执行
True:自动执行
False:手动执行(默认), 手动执行如何获取到yleid返回值
函数的手动调用: 在用例的参数里面传入固件名称
类的手动调用: 是在类的上面加上装饰器: @pytest.mark.usefixtures("固件名")
@pytest.fixture(scope="function",autouse=True) def exe(): print("执行前!") yield print("执行后!") class TestApi: def test_logi(self): print("登录") def test_registe(self): print("注册")
Fixture固件一般会结合conftest.py文件使用
1.conftest.py文件名是固定的,是fixture的容器
2.使用conftest.py文件中的fixture固件,不需要导包,直接使用即可
3.fixture固件可以有多个
4.conftest.py文件也可以有多个,并且外层的conftest.py的优先级比内层的conftest.py的优先级高
3.Pytest断言
Pytest直接使用Python原生的assert语句进行断言
断言语法: 基本形式为assert 表达式,如assert 1==2会抛出AssertionError
4.Pytest结合allure-pytest插件生成美观的Allure报告
安装allure-pytest插件
安装命令: 使用pip进行安装,命令为pip install allure-pytest
官网下载allure报告文件
官网地址:https://github.com/allure-framework/allure2/releases zip包
配置环境变量
验证:
生成临时json报告
在pytest中生成临时json报告,只需在pytest.ini文件中添加相关命
- 配置参数: 使用--alluredir指定报告存放目录,如--alluredir=temps表示报告存放在名为temps的目录下。
- 清空报告目录: 使用--clean-alluredir命令,确保每次运行测试前清空报告目录,避免报告数据累积。
- 配置好pytest.ini文件后,运行测试即可生成临时的json报告
配置命令:
根据json报告生成html的allure报告
在每次测试运行之前,需要清空上一次生成的临时json报告,以确保报告的准确性和独立性
构建allure报告: 使用allure generate命令,根据临时的json报告生成最终的html格式的allure报告。
import os
import time
import pytest
if __name__ == '__main__':
pytest.main()
time.sleep(2)
os.system("allure generate ./temps -o ./reports --clean")
5.企业级Allure报告定制
企业LOGO定制
- 目录结构:
- plugins:插件目录
- lib:依赖的jar包
- configure:配置文件目录
- bin文件夹:存放Allure执行命令和批处理文件
修改config目录下的allure.yml文件,在plugins部分加入custom-logo-plugin
- LOGO替换:
- 修改custom-logo-plugin目录下的styles.css文件:
- 将企业LOGO图片(如logo.png)放入custom−logo−plugin\static
样式调整技巧
修改后logo成果:
企业项目结构定制
@allure.epic("项目名称: lxy的测试报告")
@allure.feature("模块名称: 用户管理模块")
class TestApi:
@allure.story("登录接口")
@allure.title("验证登录接口成功返回数据")
def test_logi(self):
print("登录")
@allure.story("注册接口")
def test_registe(self):
print("注册")
- 优先级分类
- 致命(block): 表示最高优先级,问题极其严重,可能导致系统崩溃或主要功能失效。
- 严重(critical): 表示高优先级,问题较为严重,影响系统的关键功能。
- 一般(normal): 表示中等优先级,问题存在但对系统主要功能影响较小。
- 轻微(minor): 表示低优先级,问题较小,对系统影响轻微,可能是一些提示性信息。
@allure.link("http://www.baidu.com","接口访问链接")
@allure.issue("http://www.baidu.com","bug链接")
@allure.testcase("http://www.baidu.com","测试用例链接")
@allure.severity(allure.severity_level.BLOCKER)
def test_logi(self):
print("登录")
6.YAML文件详解
数据类型支持: 完美支持接口自动化所需的各种数据类型,包括整数、字符串、空值、列表、字典以及日期等复杂类型
yaml文件可用于配置文件,编写测试用例(接口自动化)
读取yaml文件
pip install pyyaml
import yaml
def read_yaml(yaml_path):
with open(yaml_path,encoding="utf-8") as f:
value = yaml.safe_load(f)
return value
if __name__ == '__main__':
print(read_yaml("../test_api/test_api.yaml"))
字典格式:
data1 : 蔡徐坤
data2 :
data3 : 及你太美
列表格式:
data4:
- data1: 鸡你太美
- data1: 鸡你太美
- data1: 鸡你太美
7. pytest的parametrize数据驱动
@pytest.mark.parametrize("参数列表", 参数数据)
参数名为字符串,参数值为列表类型
列表中有多少个值,测试用例就会执行多少次
常用写法:从yaml文件中读取测试数据,通过caseinfo参数接收数据
- 用法一(常用):
- 列表内嵌套字典结构
- 外层列表解包实现数据驱动
- 示例:@pytest.mark.parametrize("caseinfo",read_yaml("./testcases/test_api.yaml"))
- 用法二(了解):
- 列表内嵌套列表结构
- 可对内外层列表进行双重解包
- 示例:@pytest.mark.parametrize("username,password",[["data1","及你人美"],["data1","及你人美"]])