千纸鹤

  博客园  ::  :: 新随笔  ::  ::  :: 管理
  5 随笔 :: 70 文章 :: 0 评论 :: 9301 阅读
《一》淘宝电商项目: 整个Pytest课程将围绕着淘宝电商项目的应用来讲述
《二》拓展知识: Pytest快速入门教程(视频+代码):http://www.cemaxueyuan.com/series/XL100353xxxx
《三》pytest基础知识
(1)unittest和pytest的区别
(2)运行方式
Preferences > Python Integrated Tools > Testing > Default test runner(设置成Unittests),不然如果是pytest,就只会执行当前文件里的用例
(3)pytest安装
1.安装方式: pip install pytest
2.验证安装: pytest --version
3.pytest文档: 官方文档:https://docs.pytest.org/en/latest/contents.html
(4)pytest用例运行规则
1.pytest将在当前目录及其子目录中运行所有格式为test_*.py或者*_test.py文件
2.测试方法/函数默认必须是test开头
3.测试类必须是Test
4.【扩展】Python测试发现约定
a.如果未指定任何参数,则收集从testpaths (如果已配置)或当前目录开始。 另外,命令行参数可以在目录,文件名或节点ID的任何组合中使用。
b.递归到目录,除非它们匹配norecursedirs
c.在这些目录中,搜索test_*.py或*_test.py
d.从这些文件中,收集测试项目
在类之外拥有test前缀的测试函数或方法
在拥有Test前缀中的测试类(不含__init__方法)中的拥有test前缀的测试函数或方法
e.可自定义测试发现规则
pytest也可以发现使用标准的unittest.TestCase子类技术的测试用例(完全兼容unittest 的原因)
(5)pytest插件: https://docs.pytest.org/en/latest/reference/plugin_list.html

《四》pytest常用应用
(1)pytest断言: pytest的断言机制,用一句话概况就是借助python语言的运算符号和 assert关键字实现
(2)pytest常用运行参数
1.-s 用于关闭捕捉,从而输出打印信息
2.-v 用于显示具体的信息
3.-k 运行用例名称中包含某特定字符串的用例
pytest.main(['-k', 'zzz'])
4.-q 简化输出信息
pytest.main(['-q'])
5.-x 如果出现一条用例失败,则退出测试
pytest.main(['-x'])
6.指定目录以及特定的测试方法执行
pytest.main(['-s', './testing/test.py::test_222'])
7.生成测试报告:生成JUnit XML文件 测试报告 (code > Reformat Code(格式化))
pytest.main(['--junit-xml=./report/junit_report01.xml'])
pytest.main(['--html=./reoprt1.html'])
pytest.main(['-s', '--html=./reoprt2.html'])
8.用例失败控制:在第N个用例失败后,结束并退出测试执行
pytest.main(['--maxfail=2']) # 出现2个失败就终止测试
9.通过标记表达式执行
pytest.main(['-m', 'slow'])
示例
1.pytest.ini (源代码)
[pytest]
markers =
smoke: mark tests as smoke
slow
happy
serial
2.test_assert.py (源代码)
import pytest

@pytest.mark.slow
def test_kkk_01():
print('kkk')
assert 1==1

@pytest.mark.happy
def test_zzz_04():
print('kkk')
assert 1==1

if __name__ == '__main__':
# 通过标记表达式执行
pytest.main(['-m', 'slow'])
# pytest.main(['-m', 'happy'])
# pytest.main(['-m', 'not happy'])

10.多进程运行用例 (安装插件:pytest-xdist)
pytest.main(['test_many.py'])
将测试执行发送给多个CPU: pytest.main(['-n', '2', 'test_many.py'])
使用与计算机具有CPU内核一样多的进程: pytest.main(['-n','auto', 'test_many.py'])

11.重新运行失败用例 (安装插件:pytest-rerunfailures)
1.重新运行所有测试失败,指定测试的最大次数: pytest.main(['--reruns', '5', 'test_rerun.py'])
2.在每次重跑之间,增加一个延迟等待时间: pytest.main(['--reruns', '3', '--reruns-delay', '2', 'test_rerun.py'])

12.pytest的setup和teardown函数
setup和teardown可以在类的外面和里面使用
第一批次:setup_module/teardown_module:在当前文件中,在所有测试用例执行之前 与之后执行
第二批次:setup_function/teardown_function:在每个测试函数之前与之后执行
第三批次:setup/teardown:在每个测试函数之前与之后执行。这2个方法同样可以作用 于类方法
PS:执行顺序按批次顺序来,即使改变方法位置,也是一样

13.pytest配置文件: 编码格式设置:File > File Properties > File Encoding >gbk (点击Convert的转行的按钮)

14.pytest基本命令
pytest.ini: pytest.ini:作用于当前目录或者子目录,文件内容:以下五条
命令行参数,默认加到执行过程中: addopts = -s -v --html=./report/html_report.html
指定要运行的测试目录: testpaths = ./scripts
指定要运行的测试文件规则: python_files = auto*.py
指定要运行的类名规则: python_classes = Auto_* B*
指定要运行的测试用例文件名称规则: python_functions = auto_*

15.pytest常用插件 (安装插件:pytest-html)
插件列表网址:https://plugincompat.herokuapp.com/

16.pytest获取用例执行性能数据以及pytest禁用插件
查看最慢的10个用例: --durations=10
关闭插件,比如关闭doctest: -p no:doctest
pytest.ini:addopts = -s -v --html=./report/html_report.html --durations=10 -vv -p no:doctest

17.pytest跳过用例执行方法
1.有条件跳过测试函数
使用方法:@pytest.mark.skipif(condition, reason="xxx")
@pytest.mark.skipif(condition=2 > 1, reason='废弃用例,跳过')
2.无条件跳过测试函数
使用方法:@pytest.mark.skip(reason="no reason")
@pytest.mark.skip(reason='no reason')

18.pytest函数数据参数化
使用方法:@pytest.mark.parametrize(argnames,argvalues) (参数值为N个,测试方法就会运行N次)
test_multi_param.py (源代码)
import pytest

@pytest.mark.parametrize('用户名,密码',[('zz','123456'),('xz','123456'),('花儿','123456')])
def test_01(用户名,密码):
print('\n'+ 用户名)
print('\n'+ 密码)

if __name__ == '__main__':
pytest.main(['-s', 'test_multi_param.py'])


1-9:base > test_assert.py
---7:base > report > junit_report01.xml(junit_report02.xml)
---9:base > pytest.ini
10:base > test_many.py
11:base > test_rerun.py
12:base > test_setup01.py(test_setup02.py)
13:pytest_config > pytest.ini
14:pytest_config > pytest.ini
pytest_config > scripts > auto_case_01.py(auto_case_02.py、test_case_01.py)
pytest_config > test_case_01.py
16:pytest_config > pytest.ini
17:pytest_skip > test_skip.py(test_skipif.py)
18:pytest_params > test_multi_param.py(test_single_param.py)
posted on   隆江猪脚饭  阅读(100)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示