一、用例结果缩写表达方式


二、用例发现规则
测试框架在识别、加载用例的过程,称之为:用例发现pytest的用例发现步骤:
- 遍历所有的目录,例外: venv , . 开头的目录
- 打开python文件, test_ 开头 或者 _test 结尾
- 遍历所有的 Test 开头类
- 收集所有的 test_ 开头的函数 或者 方法
三、用例内容规则
pytest 8.4 增加了一个强制要求
pytest对用例的要求:
- 可调用的(函数、方法、类、对象)
- 名字 test_ 开头
- 没有参数(参数有另外含义)
- 没有返回值 (默认为None)
四、标记Mark
标记 可以让用例与众不同,进而可以让用被区别对待
python
class TestAdd:
@pytest.mark.api
def test_int(self):
res = add(1,3)
assert res == 4
@pytest.mark.ui
def test_str(self):
res = add("1","3")
assert res == "13"
@pytest.mark.pay
def test_list(self):
res = add([1],[2,3,4])
assert res == [1,2,3,4]
五、框架内置标记
框架内置标记 为用例增加特殊执行效果
和用户自定义标记区别:
- 不需注册,可以直接使用
- 不仅可以筛选,还可以增加特殊效果3. 不同的标记,增加不同的特殊效果
- skip: 无条件跳过
- skipif:有条件跳过
- xfail:预期失败
- parametrize:参数化
- usefixtures:使用fixtures
数据驱动测试 = 参数化测试 + 数据文件
根据数据文件的内容,动态决定用例的数量、内容
六、数据驱动测试参数
csv文件内容
a,b,c
1,1,2
2,3,5
3,3,6
4,4,7
- 读取内容见进行断言处理
python
@pytest.mark.ddt
@pytest.mark.parametrize(
"a,b,c",
read_csv("data.csv")
)
def test_ddt(self,a,b,c):
res = add(int(a),int(b))
assert res == int(c)
七、夹具
夹具:在用例执行之前、执行之后,自动运行代码场景:
之前:加密参数 / 之后:解密结果
之前:启动浏览器 / 之后 :关闭浏览器
之前:注册、登录账号 / 之后:删除账号
7.1 创建fixture
步骤:
- 创建函数
- 添加装饰器
- 添加yield关键字
python
@pytest.fixture
def f():
# 前置操作,写在yield 前面
yield
# 后置操作,写在yield 后面
7.2 使用fixture
- 在用例的参数列表中,加入 fixture名字即可
- 给用例加上 usefixtures 标记
python
def test_1(f):
pass
@pytest.mark.usefixtures("f")
def test_2():
pass
7.3 高级用法
- 自动使用
- 依赖使用
linux:使用linux进行编译
git:使用git进行版本控制
fixture:使用fixture进行前后置自动操作 - 返回内容:接口自动化封装:接口关联
- 范围共享:
默认范围:function
全局范围:session
使用 conftest.py
命名空间 -> 第三空间
8. 插件管理
pytest插件生态是pytest特别的优势之处。
插件分成两类:
- 不需要安装:内置插件
- 需要安装:第三方插件
插件的启用管理:
启用: -p abc
禁用: -p no:abc
插件使用方式:
- 参数
- 配置文件
- fixture
- mark
9.常用第三方插件
pytest有1400+ 插件: https://docs.pytest.org/en/stable/reference/plugin_list.html
- pytest-html
用途:生成HTML测试报告
安装:
pip install pytest-html
使用:
--html=report.html --self-contained-html

- pytest-xdist
用途:分布式执行
安装:
pip install pytest-xdist
使用:
-n N
只有在任务本身耗时较长,超出调用成本很多的时候,才有意义
分布式执行,有并发问题:资源竞争、乱序
- pytest-rerunfailures
用途:用例失败之后,重新执行
安装:
pip install pytest-rerunfailures
使用:
--reruns 5 --reruns-delay 1
- pytest-result-log
用途:把用例的执行结果记录到日志文件中
安装:
pip install pytest-result-log
使用:
python
log_file = ./logs/pytest.loglog_file_level = infolog_file_format = %(levelname)-8s %(asctime)s [%(name)s:%(lineno)s] : %
(message)s
log_file_date_format = %Y-%m-%d %H:%M:%S
; 记录用例执行结果
result_log_enable = 1
; 记录用例分割线
result_log_separator = 1
;分割线等级
result_log_level_separator = warning
;异常信息等级
result_log_level_verbose = info
10. 企业级测试报告
allure 是一个测试报告框架
安装
pip install allure-pytest
配置
python
--alluredir=temps --clean-alluredir
生成报告
allure generate -o report -c temps
allure支持对用例进行分组和关联(敏捷开发术语)
@allure.epic 史诗 项目
@allure.feature 主题 模块
@allure.story 故事 功能
@allure.title 标题 用例
使用相同装饰器的用例,自动并入一组
python
@allure.epic('自动化测试')
@allure.feature('pytest框架训练营')
@allure.story('mark标记和筛选')
@allure.title("实现筛选的用例")
@pytest.mark.utdef
test_a():
pass
@allure.epic('自动化测试')
@allure.feature('pytest框架训练营')
@allure.story('fixture前置和后置')
@allure.title("使用fixure的用例")
@pytest.mark.utdef
test_b():
pass