Pytest 基础用法
1 | # conda |
Pytest 用例命名规范
测试文件必须以 “test_” 开头(或者 “_test” 结尾)
如果直接运行文件,有目的文件,文件名不需要添加 test_ 关键字
如果运行目录,目录下的运行文件必须以 “test_” 开头(或者 “_test” 结尾)
测试方法必须以 “test_” 开头
测试类必须以 Test 开头,并且不能有 init 方法
Pytest 用例执行顺序
默认执行顺序
- 文件内:根据代码顺序执行
- 文件夹内:根据默认文件顺序执行
注意:unittest 默认执行顺序是按字符顺序执行
使用 pytest-ordering 自定义顺序
1 | # 安装 |
Pytest 常用断言类型
- 等于:
== - 不等于:
!= - 大于:
> - 小于:
< - 属于:
in - 不属于:
not in - 大于等于:
>= - 小于等于:
<= - 是:
is - 不是:
is not
Pytest + Requests
- 执行
pytest -vs .\test_request\test_request.p
1 | def test_phone_number_with_get(): |
Pytest 指定目录(文件)执行
- 不指定目录:
pytest- 根目录下查找符合条件的文件及方法
- 指定路径到目录
pytest .\test_request\- 指定目录下查找符合条件的文件及方法
- 指定路径到文件
pytest .\test_request\test_request.py - 指定文件内查找符合条件的方法
Pytest 运行参数详解
- pytest -m : 执行特定的测试用例(标签) 执行命令
pytest -m test
1 | # pytest.ini 文件中配置 |
pytest -k : 执行用例包含 关键字 的用例
先匹配文件名中的关键字,如果没有再匹配测试方法名称中的 关键字
执行命令
pytest -k phone_number
1 | def test_phone_number_with_get(): |
- pytest -q : 说明 简化控制台输出
1 | (env-pytest) D:\Business\AutoTest\ApiPytest>pytest -q test_scripts/test_mobile.py |
- pytest -v : 输出用例更加详细的执行信息
1 | (env-pytest) D:\Business\AutoTest\ApiPytest>pytest -v test_scripts/test_mobile.py |
- pytest -s : 输出用例中的调试信息
1 | (env-pytest) D:\Business\AutoTest\ApiPytest>pytest -s test_scripts/test_mobile.py |
pytest.ini配置指定运行参数
1 | # 配置默认运行参数 |
Pytest 配置参数(ini 文件)
- 创建
pytest.ini文件
1 | [pytest] |
Pytest 的 Setup 和 Teardown
- 模块级:
setup_module和teardowh_module开始于模块(每个文件)始末,生效一次
1 | def setup_module(): |
- 函数级:
setup_function和teardowh_function对不在类中的每条函数都生效
1 | def setup_function(): |
- 类级:
setup_class和teardowh_class只在类中前后生效一次,在类中
1 | class TestMobile: |
- 方法级:
setup_method和teardowh_method开始于方法始末,在类中
1 | class TestMobile: |
Pytest 的 Skip 和 Skipif
- 标签跳过
@pytest.mark.skip
1 |
|
- 条件跳过
@pytest.mark.skipif
1 | moblile = "1300000111" |
Pytest 参数依赖
注 不能使用 def __init__(self): 定义类属性
1 | class TestUser: |
Pytest 进阶用法
Pytest 进阶 fixture
fixture 是测试前后进行预备、清理工作的代码处理机制
相对于 Setup 和 Teardown 的优势:
- 命名比较灵活,局限性比较小
- conftest.py 文件配置可以实现数据共享,不需要 import 就可以自动找到一些配置
- fixture 夹具
@pytest.fixture作用范围:session > module > class > function
(scope=’function’) 每一个函数或方法都会调用
1 | # 默认 scope 是 function 即: @pytest.fixture() == @pytest.fixture(scope='function') |
(scope=’class’) 每一个类调用一次;如果 类里边的方法 都加了 fixture, 只会在第一方法执行一次
1 |
|
(scope=’module’) 每一个 .py 文件调用一次
1 |
|
(scope=’session’) 是多个文件调用一次, 需要创建 conftest.py 文件
1 | import pytest |
conftest.py文件功能- conftest.py 为固定写法,不可修改文件名字
- 使用 conftest.py 文件方法不需要导入
- 函数作用于当前文件夹及下属文件夹
1 | # -*- coding: utf-8 -*- |
conftest.py文件定义及使用
1 | # 用户登录 获取 token |
1 | # 使用:根据 token 获取用户信息 |
- yield 后置处理
1 |
|
- fixture 的执行顺序
1 |
|
fixture 的调用方法
- 可以接收返回值:@pytest.fixture(scope=’function’)
- 无法接收返回值:@pytest.mark.usefixtures(“func”)
1 |
|
- fixture 的 params 和 ids:@pytest.fixture(params = [‘参数1’, ‘参数2’],ids = [‘用例1’, ‘用例2’])
1 |
|
Pytest 数据驱动(parametrize)
1 | # 单次循环 |
Pytest 数据驱动(Yaml)
安装 conda install pyyaml 或 pip install pyyaml
语法
1 | # 字典 |
- Yaml 文件解析
1 | import yaml |
- Yaml 在线格式校验:
- Yaml + parametrize
1 | # Yaml 文件 |
1 | import pytest |
Yaml 中使用自定义函数
Yaml 文件
1 | user_info: |
Yaml 函数逻辑文件func_yaml.py
1 | # faker 生成模拟数据 |
测试脚本中使用
1 | import pytest |
Pytest 失败用例重跑
安装 conda install pytest-rerunfailures
1 |
|
Pytest 自动化框架
代码分层优化
INI 配置文件
安装 conda install configparser 或 pip install configparser
1 | # INI 配置文件 |
1 | # 解析 ini 配置文件 |
文件解析封装
1 | # -*- coding: utf-8 -*- |
接口请求封装
http_request.py
1 |
|
http_response.py
1 | # -*- coding: utf-8 -*- |
base_response.py
1 | # -*- coding: utf-8 -*- |
api_data.py
1 | # -*- coding: utf-8 -*- |
api.py
1 | # -*- coding: utf-8 -*- |
日志模块封装
1 | # -*- coding: utf-8 -*- |
Allure 测试报告
环境搭建
- 安装
conda install allure-pytest或pip install allure-pytest - Java 环境安装(jdk)
- Allure 命令行工具
- https://github.com/allure-framework/allure2/releases
- 添加
Path环境变量C:\Apps\allure-2.18.1\bin - 验证:
allure --version
运行机制
pytest.ini配置
1 | addopts : -sv --alluredir ./report |
报告查看
allure serve ./report
生成 HTML 报告: allure generate report 文件名:
allure-report生成 HTML 报告: allure generate report -o 文件名:
allure_report打开 HTML 报告: allure open allure-report 或 allure open allure_report
allure 使用方法
1 |
|
- allure 动态自定义报告
1 | import pytest |
- allure 添加环境信息
report 目录下创建 environment.properties
1 | # 字段无要求,随便写 |