Pytest框架之 - skip跳过测试用例

  • 作者:lwj
  • 分类:Pytest
  • 发表日期:2020-10-31 15:17:38
  • 阅读(280)
  • 评论(0)

自动化测试过程中, 经常会遇到功能阻塞, 功能未实现和环境等一系列外部因素问题导致的一些用例执行不了, 如果我们注释掉或删除掉, 后面可能还要进行恢复操作, 这时我们就可以用到跳过用例

Pytest存在两个跳过测试的方法:skipskipif  (注:跳过的用例测试结果标识为s)

skip

skip方法为无条件跳过测试用例(使用方法:@pytest.mark.skip)

# <------- test_three.py ------->
#!/usr/bin/python3
# coding=utf-8
# Author: 文

import pytest

class Test_One():
    @pytest.mark.skip
    def test_01(self):
        print("===========> test_01")

    def test_02(self):
        print("===========> test_02")

if __name__ == "__main__":
    pytest.main(["-rs", "test_three.py::Test_one"])

执行结果为s.(即第一个用例跳过第二个用例测试通过), 提示test_three.py第9行为无条件的跳过

F:\Python3.6.5\python.exe E:/xxx/test_three.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: E:\xxx, configfile: pytest.ini
collected 2 items
test_three.py s.                                                         [100%]
=========================== short test summary info ===========================
SKIPPED [1] test_three.py:9: unconditional skip
======================== 1 passed, 1 skipped in 0.51s =========================
Process finished with exit code 0

skipif

skipif方法为有条件跳过测试用例(使用方法:@pytest.mark.skipif(condition=跳过的条件, reason=跳过的原因))

参数condition:跳过的条件,为True则跳过测试为False则继续执行测试,默认为True

参数reason:标注跳过的原因,必填参数

# <------- test_three.py ------->
#!/usr/bin/python3
# coding=utf-8
# Author: 文

import pytest

class Test_One():
    @pytest.mark.skipif(condition=1<2, reason="参数condition为True,跳过测试")
    def test_03(self):
        print("===========> test_03")

    @pytest.mark.skipif(condition=1>2, reason="参数condition为False,不跳过测试")
    def test_04(self):
        print("===========> test_04")

if __name__ == "__main__":
    pytest.main(["-rs", "test_three.py::Test_One"])

执行结果为s. 提示test_three.py第9行为跳过理由为参数condition为True,跳过测试

F:\Python3.6.5\python.exe E:/xxx/test_three.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: E:\xxx, configfile: pytest.ini
collected 2 items
test_three.py s.                                                         [100%]
=========================== short test summary info ===========================
SKIPPED [1] test_three.py:9: 参数condition为True,跳过测试
======================== 1 passed, 1 skipped in 1.13s =========================
Process finished with exit code 0

 ~ 不积跬步无以至千里

觉得不错,支持一下!

提交评论

您尚未登录,登录之后方可评论~ 登录 or 注册

评论列表

暂无评论
返回顶部

建议反馈

1. 可在博文底部留言评论

2. 发邮件到i_suichuan@163.com