Python自动化测试全套教程-第十一天(接口自动化基础之pytest框架用法、规 则、配置、标记)
本文介绍了pytest测试框架的基本使用方法和高级特性。主要内容包括:pytest的安装与配置、测试用例编写规范(以test_开头)、执行方式和结果解读(通过.、F等符号标识测试状态)、用例发现规则等基础内容。在高级用法部分,重点讲解了配置方式(命令行参数和ini文件)和标记功能(自定义标记用于用例筛选,以及skip、xfail等内置标记的特殊效果)。文章还简要提及了参数化测试的概念,为后续学习数
文章目录
1、什么是测试框架
测试框架:抽象出来一个工具集合,提供大量组件或功能:
- 用例发现:自动化的从各目录、各文件种收集测试用例
- 用例管理:根据需求对用例进行筛选、忽略、跳过等操作
- 环境管理:在用例执行前后,自动完成某些擦着,构造合适的执行条件
- 用例执行:执行用例种的测试步
- 断言:执行用例时,判定执行结果是否符合预期
大部分的变成语言都有对应测试框架:
- Java:JUnit、TestNG
- php:phpunit
- Python:unittest、pytest
unittest:
- python内置、无需安装
- 浓郁Java风格
- 无法升级、扩展
pytest:
- 手动安装、自由切换版本
- 浓郁Python风格
- 有丰富第三方生态进行扩展
- 完全兼容unittest
2、pytest测试框架
2.1、安装pytest
pip install pytest # 安装pytest
pip install pytest -U # 安装、升级pytest 到最新版
pip install pytest==7.0 # 安装pytest 7.0版本
- pip install 安装第三方库的命令
- pytest 第三方库的名字
- -U 升级,保持最新版
- ==7.0 指定版本号
pip show pytest # 查看pyetst信息
pytest # 启动测试框架
2.2、编写测试用例
步骤如下:
- 创建: test_ 开头的py文件
- 创建: test_ 开头的函数
- 创建:assert 断言
# test_abc.py
def test_ok():
assert 1 == 1 # 测试通过
def test_fail():
assert 1 == 2 # 测试失败
2.3、执行测试用例
- 命令行
pytest
- 代码
import pytest
pytest.main() # 启动测试框架
2.4、看懂执行结果
- 执行环境
platform win32 – Python 3.12.2, pytest-8.4.1, pluggy-1.6.0 –
cachedir: .pytest_cache
rootdir: D:\tools\Pycharm\Project\api_framework
- 用例收集情况
collected 4 items
- 用例执行过程
test_tianqiu.py …F.
[100%]
缩写 | 单词 | 含义 |
---|---|---|
. | passed | 通过 |
F | failed | 失败(用例执行时报错) |
E | error | 出错(fixture执行时报错) |
s | skipped | 跳过 |
X | xpassed | 预期外的通过(不符合预期) |
x | xfailed | 预期内的失败(符合预期) |
- 用例失败原因
========================================== FAILURES=================================================
__________________________________________ test_skipif_failed_________________________________________________
def test_skipif_failed():
‘> assert 1 == 2
‘E assert 1 == 2
test_tianqiu.py:28: AssertionError
- 测试框架总结信息
======================================= short test summary info===========================================
FAILED test_tianqiu.py::test_skipif_failed - assert 1 == 2
======================================= 1 failed, 9 passed in 0.09s=========================================
2.5、用例发现规则
pytest识别、加载测试用例过程称之为用例发现,规则:
- 遍历所有的目录 (venv除外)
- 遍历所有 test_ 开头 或者 _test 结尾的python文件
- 遍历所有 Test 开头的类
- 类不能拥有 init 方法
- 收集 test_ 开头的函数或者方法,作为测试用例
重点:pytest,只有函数和方法,才被视为测试用例,目录、文件、类,作为用例的容器
3、pytest高级用法
3.1、配置
pytest 有2种配置方式:
- 命令行参数
- ini配置文件
查看所有的配置项
pytest -h
usage: pytest [options] [file_or_dir] [file_or_dir] […]
可以分成三大部分
- 参数
- 配置项
- 环境变量
常用的命令行参数:
- -v :增加详细程度
- -q :减少详细程度
- -s :不进行内容捕获,才能正常的输入输出
- -x :快速退出(冒烟测试)
常用的ini配置项:
- 在根目录种创建 pytest.ini 文件
- 创建 pytest 选择器
- 按行,添加配置项
在pytest.ini文件:
[pytest]
# 添加命令行参数
addopts = -s -x
配置是用来改变pytest
约定大于配置 对于成熟的工具来说,默认配置往往是比较好的配置,可以适用于大部分场景
如非必要,请勿更改
3.2、标记mark
mark主要用途是让用例和用例之间变得不同,实现用例的筛选
3.2.1、用户自定义标记
- 注册
[pytest]
markers =
api
ui
ut
e2e
- 标记
import pytest
@pytest.mark.ut
def test_ok():
assert 1 == 1 # 测试通过
@pytest.mark.e2e
def test_fail():
assert 1 == 2 # 测试失败
@pytest.mark.api
def test_baili():
pass
@pytest.mark.ui
def test_beifan():
pass
- 筛选
pytest -m api # 只执行拥有api标记的用例
标记支持逻辑运算
pytest -m “ut or api” #只执行拥有ut 或者 拥有api标记的用例
pytest -m “ui and api” #只执行拥有ut 并且 拥有api标记的用例
3.2.2、框架内置标记
- 不需要注册,直接使用
- 不仅用于筛选,还有特殊效果
- 不同的标记,拥有不同的效果
- skip:无条件跳过
- skipif:有条件跳过
- xfail:预期失败
- parametrize:参数化: 框架为用例传递参数
- usefixture:使用fixture(下一章)
import pytest
@pytest.mark.skip
def test_skip():
assert 1 == 2 # 失败
@pytest.mark.skipif(1 == 1,reason='跳过说明') # 跳过
def test_skipif_true():
assert 1 == 2 # 失败
@pytest.mark.skipif(1 == 2,reason='不跳过') # 不会跳过
def test_skipif_failed(): # 预期通过
assert 1 == 2 # 失败
@pytest.mark.xfail #预期失败
def test_xfail_failed():
assert 1 == 2 # 结果失败
@pytest.mark.xfail #预期失败
def test_xfail_true():
assert 1 == 1 # 结果成功
参数化测试:通过数据修改参数,从而改变测试用例
数据驱动测试 = 参数化测试 + 数据文件
参数化之前:
def add(a, b):
return a + b
def test_add_1_1():
a = 1
b = 1
assert 2 == add(a, b)
def test_add_2_2():
a = 2
b = 2
assert 4 == add(a, b)
def test_add_2_3():
a = 2
b = 3
assert 5 == add(a, b)
def test_add_3_3():
a = 3
b = 3
assert 6 == add(a, b)
参数化之后:
import pytest
def add(a,b):
return a+b
@pytest.mark.parametrize(
"a,b,c", # 1.列出参数
[ # 2.准备参数的值
[1,1,2],
[2,2,4],
[2,3,5],
[3,3,6] # 根据需求修改数据
]
)
def test_addmeth(a,b,c):
assert c == add(a,b)
练习
- 创建4个用例,通过pytest执行,得到4个结果
- 创建4个用例,通过pytest执行,得到2个结果
- 创建4个用例,通过pytest执行,得到10个结果
更多推荐
所有评论(0)