测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----如何使用skip和xfail
收藏本文
作者:redrose2100 类别: 日期:2022-05-13 18:05:04 阅读:833 次 消耗积分:0 分
## 一、显示xpass、xfail、skip用例的简要信息 pytest -rxXs 命令可以显示skip,xfail,xpass用例的简要信息 test_demo.py代码如下: ```python import pytest @pytest.mark.skip() def test_func1(): pass @pytest.mark.xfail() def test_func2(): pass @pytest.mark.skip() def test_func3(): pass def test_func4(): pass ``` 执行结果如下: ```bash $ pytest -rxXs ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 4 items test_demo.py sXs. [100%] ======================================================================= short test summary info ======================================================================== XPASS test_demo.py::test_func2 SKIPPED [1] test_demo.py:3: unconditional skip SKIPPED [1] test_demo.py:11: unconditional skip =============================================================== 1 passed, 2 skipped, 1 xpassed in 0.03s ================================================================ ``` ## 四、当导入模块失败时跳过 当在导入模块不存在或者报错时,使用如下语句可以跳过,注意需要将此语句放在导入语句的上面 test_demo.py代码如下,这里假设当前环境没有安装TensorFlow,这里导入会失败 ```python import pytest pexpect = pytest.importorskip("pexpect") import tensorflow def test_01(): assert 1==1 def test_02(): assert 2==2 ``` 执行结果如下,会直接跳过,而不会报导入错误 ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 0 items / 1 skipped ========================================================================== 1 skipped in 0.02s ========================================================================== ``` ## 五、xfail的用法 (1)xfail标记失败 test_demo.py代码如下: ```python import pytest @pytest.mark.xfail def test_01(): assert 1==1 ``` 执行结果如下: ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item test_demo.py X [100%] ========================================================================== 1 xpassed in 0.03s ========================================================================== ``` (2)根据具体条件标记失败 test_demo.py代码如下: ```python import pytest import platform def test_01(): if platform.system() == "Windows": pytest.xfail("不支持windows系统") ``` 执行结果如下: ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item test_demo.py x [100%] ========================================================================== 1 xfailed in 0.06s ========================================================================== ``` 使用pytest.mark.xfail也可以设置根据具体条件标记失败,此时需要额外增加reason参数 test_demo.py代码如下: ```python import pytest import platform @pytest.mark.xfail(platform.system() == "Windows",reason="不支持windows系统") def test_01(): assert 1==1 ``` 执行结果如下: ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item test_demo.py X [100%] ========================================================================== 1 xpassed in 0.03s ========================================================================== ``` (3)pytest.mark.xfail也可以只设置reason参数 test_demo.py代码如下: ```python import pytest @pytest.mark.xfail(reason="不支持windows系统") def test_01(): assert 1==1 ``` 执行结果如下: ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item test_demo.py X [100%] ========================================================================== 1 xpassed in 0.02s ========================================================================== ``` (4)通过run参数可以设置是否执行 test_demo.py代码如下: ```python import pytest @pytest.mark.xfail(run=False) def test_01(): print("\n in test_01...") @pytest.mark.xfail(run=True) def test_02(): print("\n in test_02...") ``` 执行结果如下: ```bash $ pytest -s ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 2 items test_demo.py x in test_02... X ==================================================================== 1 xfailed, 1 xpassed in 0.09s ===================================================================== ``` (5)xpass和xfail都不会显示测试失败,如果希望显示测试失败可以通过设置strict=True test_demo.py代码如下: ```python import pytest @pytest.mark.xfail(strict=True) def test_01(): assert 1==1 ``` 执行结果如下: ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item test_demo.py F [100%] =============================================================================== FAILURES =============================================================================== _______________________________________________________________________________ test_01 ________________________________________________________________________________ [XPASS(strict)] ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_01 ========================================================================== 1 failed in 0.03s =========================================================================== ``` 也可以在pytest.ini中设置,如pytest.ini设置如下: ```bash [pytest] xfail_strict=true ``` 此时test_demo.py代码如下: ```python import pytest @pytest.mark.xfail() def test_01(): assert 1==1 ``` 执行结果如下: ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item test_demo.py F [100%] =============================================================================== FAILURES =============================================================================== _______________________________________________________________________________ test_01 ________________________________________________________________________________ [XPASS(strict)] ======================================================================= short test summary info ======================================================================== FAILED test_demo.py::test_01 ========================================================================== 1 failed in 0.03s =========================================================================== ``` (6)当标记了xfail之后,若想使xfail失效,则可以在命令行使用 pytest --runxfail 即可 test_demo.py代码如下: ```python import pytest @pytest.mark.xfail() def test_01(): assert 1==1 ``` 执行结果如下: ```bash $ pytest --runxfail ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 1 item test_demo.py . [100%] ========================================================================== 1 passed in 0.02s =========================================================================== ``` ## 六、在参数化中使用xfail和skip test_demo.py代码如下: ```python import sys import pytest @pytest.mark.parametrize( ("n", "expected"), [ (1, 2), pytest.param(1, 0, marks=pytest.mark.xfail), pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")), (2, 3), (3, 4), (4, 5), pytest.param( 10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k") ), ], ) def test_increment(n, expected): assert n + 1 == expected ``` 执行结果如下: ```bash $ pytest ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:\src\blog\tests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 7 items test_demo.py .xx...s [100%] =============================================================== 4 passed, 1 skipped, 2 xfailed in 0.07s ================================================================ ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/193
上一篇:
Pytest----如何捕获标准输出和标准错误输出
下一篇:
Pytest----如何安装和使用插件
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件