全网最全pytest大型攻略,单元测试学这就够了
myzbx 2025-02-03 14:11 21 浏览
pytest 是一款以python为开发语言的第三方测试,主要特点如下:
- 比自带的 unittest 更简洁高效,兼容 unittest框架
- 支持参数化
- 可以更精确的控制要测试的测试用例
- 丰富的插件,已有300多个各种各样的插件,也可自定义扩展,如pytest-selenium、pytest-html、pytest-rerunfailures、pytes-xdish
- 可很好的和CI工具结合
安装
pip install pytest
测试用例编写规则
- 测试文件以test_开头 或者 _test结尾
- 测试类以Test开头,并且不能带有 init 方法
- 测试文件以 test_开头
- 断言使用基本的 assert 即可
pytest会递归查找当前目录及子目录下所有 以test_开始 或者 _test结尾的python脚本,执行其中符合规则的函数和方法,不需要显示调用
运行命令:(cmd进入用例所在目录)
pytest folder_name ======》直接运行文件夹内符合规则的所有用例
pytest test_file.py ======》执行某个py文件中的用例
pytest test_file.py::test_func ======》执行模块内的某个函数(节点运行)
pytest test_file.py::TestClass::test_method ======》执行模块内测试类的某个方法(节点运行)
pytest test_file.py::TestClass ======》执行模块内某个测试类(节点运行)
pytest test_file.py::TestClass test_file2.py::test_mothod ======》多节点运行,中间用空格隔开
pytest -k pass ======》匹配用例名称的表达式,含有“pass”的被执行,其他的deselected
pytest -k "pass or fail" ======》组合匹配,含有“pass” 和 “fail”的被执行
pytest -k "not pass" ======》排除运行,不含“pass”的被执行
pytest -m finished ======》标记表达式,运行用@pytest.mark.finished 标记的用例
pytest -m "finished and not merged" ======》多个标记逻辑匹配,运行含有finished 不含 merged标记的用例
pytest -v ======》运行时显示详细信息
pytest -s ======》显示打印消息
pytest -x ======》遇到错误就停止运行
pytest -x --maxfail=2 ======》遇到两个错误就停止运行
pytest --setup-show ======》跟踪固件运行
pytest -v --reruns 5 --reruns-delay 1 ======》运行失败的用例间隔1s重新运行5次 pip install pytest-rerunfailures
pytest ======》多条断言,报错后,后面的依然执行, pip install pytest-assume,断言 pytest.assume(2==4)
pytest -n 3 ======》3个cpu并行执行测试用例,需保证测试用例可随机执行, pip install pytest-xdist分布式执行插件,多个cpu或主机执行
pytest -v -n auto ======》自动侦测系统里cpu的数目
pytest --count=2 ======》重复运行测试 pip install pytest-repeat
pytest --html=./report/report.html ======》生成报告,此报告中css是独立的,分享时会丢失样式,pip install pytest-html
pytest --html=report.html --self-containd-html ======》合并css到html报告中,除了passed所有行都被展开
pytest --durations=10 ======》获取最慢的10个用例的执行耗时
用例执行顺序控制
pytest 用例执行顺序默认是按字母顺序去执行,要控制执行顺序,需要安装插件 pytest-ordering:pip install pytest-ordering
在测试方法上加上装饰器:
@pytest.mark.last 最后一个执行
@pytest.mark.run(order=n) n=1则是第一个执行
Mark
标签的使用方法:
注册标签名 / 内置标签—> 在测试用例 / 测试类 / 模块文件 前面加上 @pytest.mark.标签名
注册方法:
1.在conftest.py 文件中添加代码
# 单个标签文件内容
def pytest_configure(config):
config.addinivalue_line("markers", "demo:demo标签名称")
# 多个标签文件内容
def pytest_configure(config):
marker_list = ["p0:p0级别用例", "p1:p1级别用例", "p2:p2级别用例"] # 标签名称
for markers in marker_list:
config.addinivalue_line("markers", markers)
2.项目中添加pytest.ini配置文件
[pytest]
markers =
p0:p0级别用例
p1:p1级别用例
p2:p2级别用例
使用方法:
import pytest
@pytest.mark.p0
def test_mark01():
print("函数级别的mark_p0")
@pytest.mark.p1
def test_mark02():
print("函数级别的mark_p1")
@pytest.mark.P2
class TestDemo:
def test_mark03(self):
print("mark_p2")
def test_mark04(self):
print("mark_p2")
运行方式:
命令行运行
pytest -m "p0 and p1"
文件运行
pytest.main(["-m", "P0", "--html=report.html"])
内置标签:
参数化:@pytest.mark.parametrize(argnames, argvalues)
无条件跳过用例:@pytest.mark.skip(reason=“xxx”)
有条件跳过用例:@pytest.mark.skipif(version < 0.3, reason = “not supported until 0.3”)
预测执行失败进行提示标记:@pytest.mark.xfail(version < 0.3, reason = “not supported until 0.3”),运行结果为X(通过xpassed,失败xfailed)
# 参数化
import hashlib
@pytest.mark.parametrize("x", list(range(10)))
def test_somethins(x):
time.sleep(1)
@pytest.mark.parametrize("passwd",["123456", "abcdefgfs", "as52345fasdf4"])
def test_passwd_length(passwd):
assert len(passwd) >= 8
@pytest.mark.parametrize('user, passwd',[('jack', 'abcdefgh'),('tom', 'a123456a')])
def test_passwd_md5(user, passwd):
db = {
'jack': 'e8dc4081b13434b45189a720b77b6818',
'tom': '1702a132e769a623c1adb78353fc9503'
}
assert hashlib.md5(passwd.encode()).hexdigest() == db[user]
# 如果觉得每组测试的默认参数显示不清晰,可以使用 pytest.param 的 id 参数进行自定义
@pytest.mark.parametrize("user, passwd",
[pytest.param("jack", "abcdefgh", id = "User<Jack>"),
pytest.param("tom", "a123456a", id = "User<Tom>")])
def test_passwd_md5_id(user, passwd):
db = {
'jack': 'e8dc4081b13434b45189a720b77b6818',
'tom': '1702a132e769a623c1adb78353fc9503'
}
assert hashlib.md5(passwd.encode()).hexdigest() == db[user]
Fixture
固件:是一些函数,pytest会在执行函数之前或者之后加载运行它们,相当于预处理和后处理。
fixture的目的是提供一个固定基线,在该基线上测试可以可靠地、重复的执行。
名称:默认为定义时的函数名,可以通过 @pytest.fixture(name="demo") 给fixture重命名
定义:在固件函数定义前加上@pytest.fixture();fixture是有返回值的,没return则返回None
使用:作为参数、使用usefixtures、自动执行(定义时指定autouse参数)
def test_demo(fixture_func_name)
@pytest.mark.usefixtures("fixture_func_name1", "fixture_func_name2") 标记函数或者类
预处理和后处理:用yield关键词,yield之前的代码是预处理,之后的是后处理
作用域:通过scope参数控制作用域
function:函数级,每个测试函数都会执行一次(默认)
class:类级别,每个测试类执行一次,所有方法都共享这个fixture
module:模块级别,每个模块.py执行一次,模块中所有测试函数、类方法 或者 其他fixture 都共享这个fixture
session:会话级别,每次会话只执行一次,一次会话中所有的函数、方法都共享这个fixture
集中管理:使用文件conftest.py 集中管理,在不同层级定义,作用于在其所在的目录和子目录,pytest会自动调用
scope、yield、auto的使用
# scope、yield、auto使用
@pytest.fixture(scope = "function", autouse=True)
def function_scope():
pass
@pytest.fixture(scope = "module", autouse=True)
def module_scope():
pass
@pytest.fixture(scope = "session")
def session_scope():
pass
@pytest.fixture(scope = "class", autouse=True)
def class_scope():
pass
import time
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
@pytest.fixture(scope='session', autouse=True)
def timer_session_scope():
start = time.time()
print('\nsession start: {}'.format(time.strftime(DATE_FORMAT, time.localtime(start))))
yield
finished = time.time()
print('\nsession finished: {}'.format(time.strftime(DATE_FORMAT, time.localtime(finished))))
print('session Total time cost: {:.3f}s'.format(finished - start))
def test_1():
time.sleep(1)
def test_2():
time.sleep(2)
'''
执行命令:pytest --setup-show -s
固件执行结果:
test_pytest_study.py
session start: 2020-04-16 17:29:02
SETUP S timer_session_scope
SETUP M module_scope
SETUP C class_scope
SETUP F function_scope
test_pytest_study.py::test_3 (fixtures used: class_scope, function_scope, module_scope, timer_session_scope).
TEARDOWN F function_scope
TEARDOWN C class_scope
SETUP C class_scope
SETUP F function_scope
test_pytest_study.py::test_4 (fixtures used: class_scope, function_scope, module_scope, timer_session_scope).
TEARDOWN F function_scope
TEARDOWN C class_scope
TEARDOWN M module_scope
session finished: 2020-04-16 17:29:05
session Total time cost: 3.087s
TEARDOWN S timer_session_scope
'''
使用文件conftest.py 集中管理
# conftest.py
# conding=utf-8
import pytest
@pytest.fixture()
def postcode():
print("执行postcode fixture")
return "010"
# test_demo.py
# coding=utf-8
import pytest
class TestDemo():
def test_postcode(self, postcode):
assert postcode == "010"
if __name__=="__main__":
pytest.main(["--setup-show", "-s", "test_demo.py"])
python test_demo.py
执行过程:
test_demo.py 执行postcode fixture
SETUP F postcode
test_demo.py::TestDemo::test_postcode (fixtures used: postcode).
TEARDOWN F postcode
# 如果整个文件都用一个fixture,可以用pytestmark标记
pytestmark = pytest.mark.usefixtures("login")
fixture参数化
固件参数化需要使用pytest内置的固件request,并通过 request.param 获取参数。
# test_demo.py
@pytest.fixture(params=[
("user1", "passwd1"),
("user2", "passwd2")
])
def param(request):
return request.param
@pytest.fixture(autouse=True)
def login(param):
print("\n登录成功 %s %s" %param)
yield
print("\n退出成功 %s %s" %param)
def test_api():
assert 1 == 1
'''
pytest -s -v test_demo.py
运行结果:
test_demo.py::test_api[param0]
登录成功 user1 passwd1
PASSED
退出成功 user1 passwd1
test_demo.py::test_api[param1]
登录成功 user2 passwd2
PASSED
退出成功 user2 passwd2
'''
assert
assert "h" in "hello"
assert 3==4
assert 3!=4
assert f()==4
assert 5>6
assert not xx
assert {"0", "1", "2"} == {"0", "1", "2"}
相关推荐
- MORROR ART:毫无音质可言,真的只是好看而已...
-
今天早上我在微博上发了一条短视频,内容是某款网红音箱正在放声歌唱——这玩意就是此前曾经在网上挺火的所谓“悬浮歌词音箱”。这款产品是我同事收到的礼品,但她嫌在家里放着没用,所以拿到公司里做我们的拍摄道具...
- 「JS优化篇」你的 if - else 代码肯定没我写的好
-
作者:小生方勤转发链接:https://mp.weixin.qq.com/s/JzOQ_OwAYoP5Ic1VBtCZNA前言最近部门在对以往的代码做一些优化,我在代码中看到一连串的if(){}el...
- 细聊微内核架构在前端的应用「干货」
-
作者:semlinker转发链接:https://mp.weixin.qq.com/s/ywc98dS4TVB4t3L2tIyk8g一、微内核架构简介1.1微内核的概念微内核架构(Microke...
- ThreeJS 入门教程(一) 是选择桌面的固守还是云原生?
-
导读:最近我购置了一台新的电脑,硬盘空间只有1T。我很担心这个电脑还能用多久。性能限制或者空间的限制,都使得在未来3-5年内,这个电脑会被淘汰。但是,基于云APP的使用,老的电脑是足够了,而且,我们也...
- 推荐三款正则可视化工具「JS篇」(正则在线调试)
-
作者:代码先森转发链接:https://mp.weixin.qq.com/s/rw29yKBwti5sIsx2GKG9pw前言最近老王对可视化非常着迷。例如,算法可视化、正则可视化、Vue数据劫持可...
- Javascript 多线程编程的前世今生
-
作者:jolamjiang腾讯技术工程转发链接:https://mp.weixin.qq.com/s/87C9GAFb0Y_i5iPbIL5Hzg为什么要多线程编程大家看到文章的标题《Javasc...
- Pug 3.0.0正式发布,不再支持 Node.js 6/8
-
作者:李俊辰前端之巅转发链接:https://mp.weixin.qq.com/s/q-49Gf-SFijeu7d2MqztIQ前言近日,Pug3.0.0正式发布,Pug原名Jade,是由...
- 36个工作中常用的JavaScript函数片段「值得收藏」
-
作者:Eno_Yao转发链接:https://segmentfault.com/a/1190000022623676前言如果文章和笔记能带您一丝帮助或者启发,请不要吝啬你的赞和收藏,你的肯定是我前进的...
- 深入JavaScript教你内存泄漏如何防范
-
作者:大道至简转发链接:https://mp.weixin.qq.com/s/0w6aWwpR3MAJnmyLwDnAzA前言一般情况下,忽视内存管理不会对传统的网页产生显著的后果。这是因为,用户刷新...
- 由浅入深,66条JavaScript面试知识点(七)
-
作者:JakeZhang转发链接:https://juejin.im/post/5ef8377f6fb9a07e693a6061目录由浅入深,66条JavaScript面试知识点(一)由浅入深,66...
- 用STM32做了个电子秤,成本仅两位数,精度高!解析一下原理
-
俗话说得好!人在胖,秤在看!所以,我想DIY一个精度高的体重秤!并希望它不只能称体重:还能像这样称克重(可设置KG,G,最低可称100克)……这样一来,做甜品的时候,还能拿来应应急。保姆级教程,记录在...
- 前端开发需要了解常用7种JavaScript设计模式
-
作者|Deven译者|王强策划|小智转发链接:https://mp.weixin.qq.com/s/Lw4D7bfUSw_kPoJMD6W8gg前言JavaScript中的设计模式指的是...
- 毛姆的一个手法|王培军(毛姆作品简介)
-
鲁本斯画《海伦娜·芙尔曼肖像》钱锺书在《宋诗选注》文同小传中说:“具体的把当前风物比拟为某种画法或某某大画家的名作”,是“从文同正式起头”。如钱先生所举的:“峰峦李成似,涧谷范宽能”,“独坐水轩人不到...
- 欣赏 | 朝戈:我渴望找到直达心灵的永恒
-
朋友,通过艺术让我们共同感知世界的永恒与不朽。——朝戈橙色的人物117X71cm布面油画2003包与陈185cm×103cm2007年白色80cm×40cm2009年光布面油画-Light-Oilo...
- Web页面如此耗电!到了某种程度,会是大损失
-
现在用户上网大多使用移动设备或者笔记本电脑。对这两者来说,电池寿命都很重要。在这篇文章里,我们将讨论影响电池寿命的因素,以及作为一个web开发者,我们如何让网页耗电更少,以便用户有更多时间来关注我们的...
- 一周热门
- 最近发表
- 标签列表
-
- HTML 简介 (30)
- HTML 响应式设计 (31)
- HTML URL 编码 (32)
- HTML Web 服务器 (31)
- HTML 表单属性 (32)
- HTML 音频 (31)
- HTML5 支持 (33)
- HTML API (36)
- HTML 总结 (32)
- HTML 全局属性 (32)
- HTML 事件 (31)
- HTML 画布 (32)
- HTTP 方法 (30)
- 键盘快捷键 (30)
- CSS 语法 (35)
- CSS 选择器 (30)
- CSS 轮廓 (30)
- CSS 轮廓宽度 (31)
- CSS 谷歌字体 (33)
- CSS 链接 (31)
- CSS 中级教程 (30)
- CSS 定位 (31)
- CSS 图片库 (32)
- CSS 图像精灵 (31)
- SVG 文本 (32)