如果你还想从头学起pytest,可以看看这个系列的文章哦!
https://www.cnblogs.com/poloyy/category/1690628.html
前言 可以标记无法在某些平台上运行的测试功能,戒者您希望失败的测试功能pytest.mark.skip
希望满足某些条件才执行某些测试用例,否则pytest会跳过运行该测试用例实际常见场景:跳过非Windows平台上的仅Windows测试,或者跳过依赖于当前不可用的外部资源(例如数据库)的测试@pytest.mark.skip跳过执行测试用例,有可选参数reason:跳过的原因,会在执行结果中打印
代码语言:javascript代码运行次数:0运行复制<code class="javascript">#!/usr/bin/env python# -*- coding: utf-8 -*-"""__title__ = __Time__ = 2020/4/9 13:49__Author__ = 小菠萝测试笔记__Blog__ = https://www.cnblogs.com/poloyy/"""import pytest@pytest.fixture(autouse=True)def login(): print("====登录====")def test_case01(): print("我是测试用例11111")@pytest.mark.skip(reason="不执行该用例!!因为没写好!!")def test_case02(): print("我是测试用例22222")class Test1: def test_1(self): print("%% 我是类测试用例1111 %%") @pytest.mark.skip(reason="不想执行") def test_2(self): print("%% 我是类测试用例2222 %%")@pytest.mark.skip(reason="类也可以跳过不执行")class TestSkip: def test_1(self): print("%% 不会执行 %%")</code>
@pytest.mark.skip
如果加在类上面,类里面的所有测试用例都不会执行以上小案例都是针对:整个测试用例方法跳过执行,如果想在测试用例执行期间跳过不继续往下执行呢?pytest.skip()函数基础使用作用:在测试用例执行期间强制跳过不再执行剩余内容
类似:在Python的循环里面,满足某些条件则break 跳出循环
代码语言:javascript代码运行次数:0运行复制<code class="javascript">def test_function(): n = 1 while True: print(f"这是我第{n}条用例") n += 1 if n == 5: pytest.skip("我跑五次了不跑了")</code>
当 allow_module_level=True 时,可以设置在模块级别跳过整个模块
代码语言:javascript代码运行次数:0运行复制<code class="javascript">#!/usr/bin/env python# -*- coding: utf-8 -*-"""__title__ = __Time__ = 2020/4/9 13:49__Author__ = 小菠萝测试笔记__Blog__ = https://www.cnblogs.com/poloyy/"""import sysimport pytestif sys.platform.startswith("win"): pytest.skip("skipping windows-only tests", allow_module_level=True)@pytest.fixture(autouse=True)def login(): print("====登录====")def test_case01(): print("我是测试用例11111")</code><code class="javascript">collecting ... Skipped: skipping windows-only testscollected 0 items / 1 skipped============================= 1 skipped in 0.15s ==============================</code>
作用:希望有条件地跳过某些测试用例
注意:condition需要返回True才会跳过
代码语言:javascript代码运行次数:0运行复制<code class="javascript">@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")class TestSkipIf(object): def test_function(self): print("不能在window上运行")</code><code class="javascript">collecting ... collected 1 item07skip_sipif.py::TestSkipIf::test_function SKIPPED [100%]Skipped: does not run on windows============================= 1 skipped in 0.04s ==============================</code>
pytest.mark.skip
在不同模块之间共享这个标记变量若有多个模块的测试用例需要用到相同的 或 skipif ,可以用一个单独的文件去管理这些通用标记,然后适用于整个测试用例集skip
代码语言:javascript代码运行次数:0运行复制<code class="javascript"># 标记skipmark = pytest.mark.skip(reason="不能在window上运行=====")skipifmark = pytest.mark.skipif(sys.platform == 'win32', reason="不能在window上运行啦啦啦=====")@skipmarkclass TestSkip_Mark(object): @skipifmark def test_function(self): print("测试标记") def test_def(self): print("测试标记")@skipmarkdef test_skip(): print("测试标记")</code><code class="javascript">collecting ... collected 3 items07skip_sipif.py::TestSkip_Mark::test_function SKIPPED [ 33%]Skipped: 不能在window上运行啦啦啦=====07skip_sipif.py::TestSkip_Mark::test_def SKIPPED [ 66%]Skipped: 不能在window上运行=====07skip_sipif.py::test_skip SKIPPED [100%]Skipped: 不能在window上运行================================== 3 skipped in 0.04s ==============================</code>
作用:如果缺少某些导入,则跳过模块中的所有测试
参数列表
modname:模块名minversion:版本号reasone:跳过原因,默认不给也行代码语言:javascript代码运行次数:0运行复制<code class="javascript">pexpect = pytest.importorskip("pexpect", minversion="0.3")@pexpectdef test_import(): print("test")</code><code class="javascript">Skipped: could not import 'pexpect': No module named 'pexpect'collected 0 items / 1 skipped</code>
<code class="javascript">Skipped: module 'sys' has __version__ None, required is: '0.3'collected 0 items / 1 skipped</code>
以上就是Pytest系列(7) - skip、skipif跳过用例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号