pytest入门到熟练

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

二、用例发现规则

测试框架在识别、加载用例的过程,称之为:用例发现pytest的用例发现步骤:

  1. 遍历所有的目录,例外: venv , . 开头的目录
  2. 打开python文件, test_ 开头 或者 _test 结尾
  3. 遍历所有的 Test 开头类
  4. 收集所有的 test_ 开头的函数 或者 方法

三、用例内容规则

pytest 8.4 增加了一个强制要求

pytest对用例的要求:

  1. 可调用的(函数、方法、类、对象)
  2. 名字 test_ 开头
  3. 没有参数(参数有另外含义)
  4. 没有返回值 (默认为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]

五、框架内置标记

框架内置标记 为用例增加特殊执行效果

和用户自定义标记区别:

  1. 不需注册,可以直接使用
  2. 不仅可以筛选,还可以增加特殊效果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

步骤:

  1. 创建函数
  2. 添加装饰器
  3. 添加yield关键字
python 复制代码
@pytest.fixture
def f():
# 前置操作,写在yield 前面
yield
# 后置操作,写在yield 后面

7.2 使用fixture

  1. 在用例的参数列表中,加入 fixture名字即可
  2. 给用例加上 usefixtures 标记
python 复制代码
def test_1(f):
pass
@pytest.mark.usefixtures("f")
def test_2():
pass

7.3 高级用法

  1. 自动使用
  2. 依赖使用
    linux:使用linux进行编译
    git:使用git进行版本控制
    fixture:使用fixture进行前后置自动操作
  3. 返回内容:接口自动化封装:接口关联
  4. 范围共享:
    默认范围:function
    全局范围:session
    使用 conftest.py
    命名空间 -> 第三空间

8. 插件管理

pytest插件生态是pytest特别的优势之处。

插件分成两类:

  • 不需要安装:内置插件
  • 需要安装:第三方插件
    插件的启用管理:
    启用: -p abc
    禁用: -p no:abc
    插件使用方式:
  1. 参数
  2. 配置文件
  3. fixture
  4. mark

9.常用第三方插件

pytest有1400+ 插件: https://docs.pytest.org/en/stable/reference/plugin_list.html

  1. pytest-html
    用途:生成HTML测试报告
    安装:

pip install pytest-html

使用:

--html=report.html --self-contained-html

  1. pytest-xdist

用途:分布式执行

安装:

pip install pytest-xdist

使用:

-n N

只有在任务本身耗时较长,超出调用成本很多的时候,才有意义

分布式执行,有并发问题:资源竞争、乱序

  1. pytest-rerunfailures
    用途:用例失败之后,重新执行
    安装:

pip install pytest-rerunfailures

使用:

--reruns 5 --reruns-delay 1

  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
相关推荐
专职10 小时前
pytest+requests+allure生成接口自动化测试报告
开发语言·python·pytest
小熊出擊1 天前
【pytest】fixture 内省(Introspection)测试上下文
python·单元测试·pytest
小熊出擊2 天前
【pytest】finalizer 执行顺序:FILO 原则
python·测试工具·单元测试·pytest
小熊出擊3 天前
【pytest】使用 marker 向 fixture 传递数据
python·pytest
wa的一声哭了3 天前
Deep Learning Optimizer | Adam、AdamW
人工智能·深度学习·神经网络·机器学习·自然语言处理·transformer·pytest
专职5 天前
pytest生成测试用例,allure生成测试报告
测试用例·pytest
小丁爱养花8 天前
接口自动化测试 - pytest [1]
python·自动化·pytest
向上的车轮10 天前
如何用AI工具开发一个轻量化CRM系统(七):AI生成pytest测试脚本
pytest
慌糖10 天前
自动化接口框架搭建分享-pytest第三部分
运维·自动化·pytest