Novice学Pytest(7)-skip和skipif跳过用例

一、前言

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

二、@pytest.mark.skip

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 """
 5 __Title__   =
 6 __Time__    =  2021/8/15 15:22
 7 __Author__  =  Isaymore
 8 __Blog__    =  https://www.cnblogs.com/huainanhai/
 9 """
10 
11 import pytest
12 
13 @pytest.fixture(autouse=True)
14 def login():
15     print("===登录===")
16 
17 def test_send_gift():
18     print("===登录后才能送礼===")
19 
20 @pytest.mark.skip(reason="不执行该用例!因为我还没写好!")
21 def test_attention():
22     print("===登录后才能关注===")
23 
24 class Testone:
25     def test_case_one(self):
26         print("===我是类测试用例111===")
27 
28     @pytest.mark.skip(reason="不想执行")
29     def test_case_two(self):
30         print("===我是类测试用例222===")
31 
32 @pytest.mark.skip(reason="类也可以跳过不执行")
33 class TestSkip:
34     def test_1(self):
35         print("===不会执行===")

  执行结果:

  知识点:

  • @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.skip(msg="",allow_module_lever=False),当 allow_module_level=True 时,可以设置在模块级别跳过整个模块

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 """
 5 __Title__   =
 6 __Time__    =  2021/8/15 16:05
 7 __Author__  =  Isaymore
 8 __Blog__    =  https://www.cnblogs.com/huainanhai/
 9 """
10 
11 import sys
12 import pytest
13 
14 if sys.platform.startswith("win"):
15     pytest.skip("skipping windows-only test",allow_module_level=True)
16 
17 @pytest.fixture(autouse=True)
18 def login():
19     print("===登录===")
20 
21 def test_case_one():
22     print("===我是测试用例111===")

 执行结果:

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

  作用:希望有条件地跳过某些测试用例

  注意:condition需要返回True才会跳过

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

  执行结果:

四、跳过标记

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

  执行结果:

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

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

  参数列表

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

  执行结果:找不到module

  

1 sys = pytest.importorskip("sys",minversion="0.3")
2 
3 @sys
4 def test_import_sys():
5     print("===import sys module===")

  执行结果:最小版本对应不上

 

参考链接:https://www.cnblogs.com/poloyy/p/12666682.html

posted @ 2022-04-22 22:45  方缘  阅读(76)  评论(0编辑  收藏  举报