前言

  • pytest.mark.skip 可以标记无法在某些平台上运行的测试功能,戒者您希望失败的测试功能
  • 希望满足某些条件才执行某些测试用例,否则pytest会跳过运行该测试用例
  • 实际常见场景:跳过非Windows平台上的仅Windows测试,或者跳过依赖于当前不可用的外部资源(例如数据库)的测试

@pytest.mark.skip

跳过执行测试用例,有可选参数reason:跳过的原因,会在执行结果中打印

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ =
  5. __Time__ = 2020/4/9 13:49
  6. __Author__ = 小菠萝测试笔记
  7. __Blog__ = https://www.cnblogs.com/poloyy/
  8. """
  9. import pytest
  10. @pytest.fixture(autouse=True)
  11. def login():
  12. print("====登录====")
  13. def test_case01():
  14. print("我是测试用例11111")
  15. @pytest.mark.skip(reason="不执行该用例!!因为没写好!!")
  16. def test_case02():
  17. print("我是测试用例22222")
  18. class Test1:
  19. def test_1(self):
  20. print("%% 我是类测试用例1111 %%")
  21. @pytest.mark.skip(reason="不想执行")
  22. def test_2(self):
  23. print("%% 我是类测试用例2222 %%")
  24. @pytest.mark.skip(reason="类也可以跳过不执行")
  25. class TestSkip:
  26. def test_1(self):
  27. print("%% 不会执行 %%")

执行结果

Pytest系列(7) - skip、skipif跳过用例 - 图1

知识点

  • @pytest.mark.skip 可以加在函数上,类上,类方法上
  • 如果加在类上面,类里面的所有测试用例都不会执行
  • 以上小案例都是针对:整个测试用例方法跳过执行,如果想在测试用例执行期间跳过不继续往下执行呢?

pytest.skip()函数基础使用

作用:在测试用例执行期间强制跳过不再执行剩余内容
类似:在Python的循环里面,满足某些条件则break 跳出循环

  1. def test_function():
  2. n = 1
  3. while True:
  4. print(f"这是我第{n}条用例")
  5. n += 1
  6. if n == 5:
  7. pytest.skip("我跑五次了不跑了")

执行结果

Pytest系列(7) - skip、skipif跳过用例 - 图2

pytest.skip(msg=””,allow_module_level=False)

当 allow_module_level=True 时,可以设置在模块级别跳过整个模块

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ =
  5. __Time__ = 2020/4/9 13:49
  6. __Author__ = 小菠萝测试笔记
  7. __Blog__ = https://www.cnblogs.com/poloyy/
  8. """
  9. import sys
  10. import pytest
  11. if sys.platform.startswith("win"):
  12. pytest.skip("skipping windows-only tests", allow_module_level=True)
  13. @pytest.fixture(autouse=True)
  14. def login():
  15. print("====登录====")
  16. def test_case01():
  17. print("我是测试用例11111")

执行结果

  1. collecting ...
  2. Skipped: skipping windows-only tests
  3. collected 0 items / 1 skipped
  4. ============================= 1 skipped in 0.15s ==============================

@pytest.mark.skipif(condition, reason=””)

作用:希望有条件地跳过某些测试用例
注意:condition需要返回True才会跳过

  1. @pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
  2. class TestSkipIf(object):
  3. def test_function(self):
  4. print("不能在window上运行")

执行结果

  1. collecting ... collected 1 item
  2. 07skip_sipif.py::TestSkipIf::test_function SKIPPED [100%]
  3. Skipped: does not run on windows
  4. ============================= 1 skipped in 0.04s ==============================

跳过标记

  • 可以将 pytest.mark.skip 和 pytest.mark.skipif 赋值给一个标记变量
  • 在不同模块之间共享这个标记变量
  • 若有多个模块的测试用例需要用到相同的 skip 或 skipif ,可以用一个单独的文件去管理这些通用标记,然后适用于整个测试用例集
  1. # 标记
  2. skipmark = pytest.mark.skip(reason="不能在window上运行=====")
  3. skipifmark = pytest.mark.skipif(sys.platform == 'win32', reason="不能在window上运行啦啦啦=====")
  4. @skipmark
  5. class TestSkip_Mark(object):
  6. @skipifmark
  7. def test_function(self):
  8. print("测试标记")
  9. def test_def(self):
  10. print("测试标记")
  11. @skipmark
  12. def test_skip():
  13. print("测试标记")

执行结果

  1. collecting ... collected 3 items
  2. 07skip_sipif.py::TestSkip_Mark::test_function SKIPPED [ 33%]
  3. Skipped: 不能在window上运行啦啦啦=====
  4. 07skip_sipif.py::TestSkip_Mark::test_def SKIPPED [ 66%]
  5. Skipped: 不能在window上运行=====
  6. 07skip_sipif.py::test_skip SKIPPED [100%]
  7. Skipped: 不能在window上运行=====
  8. ============================= 3 skipped in 0.04s ==============================

pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )

作用:如果缺少某些导入,则跳过模块中的所有测试
参数列表

  • modname:模块名
  • minversion:版本号
  • reasone:跳过原因,默认不给也行
  1. pexpect = pytest.importorskip("pexpect", minversion="0.3")
  2. @pexpect
  3. def test_import():
  4. print("test")

执行结果一:如果找不到module

  1. Skipped: could not import 'pexpect': No module named 'pexpect'
  2. collected 0 items / 1 skipped

执行结果一:如果版本对应不上

  1. Skipped: module 'sys' has __version__ None, required is: '0.3'
  2. collected 0 items / 1 skipped

转载:https://www.cnblogs.com/poloyy/p/12666682.html