Pytest-mark标记的其他方法

简介: Pytest-mark标记的其他方法

在前面的文章中,讲了mark的自定义标记以及参数化,fixture的手动调用。本章就主要介绍介绍剩下的内容。

skip跳过用例

1、函数/方法级跳过

import pytest
def test_01():
    print("--01--")
@pytest.mark.skip("无条件跳过")
def test_02():
    print("--02--")
def test_03():
    print("--03--")
"""
Case/test_a.py::test_01 --01--
PASSED
Case/test_a.py::test_02 SKIPPED (无条件跳过)
Case/test_a.py::test_03 --03--
PASSED
"""

2、模块级跳过

"""test_a.py"""
import pytest
pytest.skip('这个模块不测了',allow_module_level=True)
def test_01():
    print("--01--")
def test_02():
    print("--02--")
def test_03():
    print("--03--")
"""test_c.py"""
def test_01():
    print("--01--")
Case/test_c.py::test_01 --01--
PASSED
======================== 1 passed, 1 skipped in 0.02s =========================

看到了吗,test_a.py直接被跳过了,只执行了test_c.py文件中的用例。

3、类级跳过

import pytest
def test_01():
    print("--01--")
def test_02():
    print("--02--")
def test_03():
    print("--03--")
@pytest.mark.skip("这个类,我跳过了")
class Test_a:
    def test_method(self):
        print("--method--")
"""
Case/test_a.py::test_01 --01--
PASSED
Case/test_a.py::test_02 --02--
PASSED
Case/test_a.py::test_03 --03--
PASSED
Case/test_a.py::Test_a::test_method SKIPPED (这个类,我跳过了)
"""

skipif条件跳过

1、类级跳过

import pytest
def test_01():
    print("--01--")
def test_02():
    print("--02--")
def test_03():
    print("--03--")
@pytest.mark.skipif(1>0,reason="条件为真,这个类,我跳过了")
class Test_a:
    def test_method(self):
        print("--method--")
"""
Case/test_a.py::test_01 --01--
PASSED
Case/test_a.py::test_02 --02--
PASSED
Case/test_a.py::test_03 --03--
PASSED
Case/test_a.py::Test_a::test_method SKIPPED (条件为真,这个类,我跳过了)
======================== 3 passed, 1 skipped in 0.03s =========================
"""

2、方法/函数级跳过

import pytest
def test_01():
    print("--01--")
def test_02():
    print("--02--")
@pytest.mark.skipif(1>0,reason="条件为真,这个函数,我跳过了")
def test_03():
    print("--03--")
class Test_a:
    def test_method(self):
        print("--method--")
"""
Case/test_a.py::test_01 --01--
PASSED
Case/test_a.py::test_02 --02--
PASSED
Case/test_a.py::test_03 SKIPPED (条件为真,这个函数,我跳过了)
Case/test_a.py::Test_a::test_method --method--
PASSED
"""

3、模块级跳过

"""test_a.py"""
import pytest
pytestmark = pytest.mark.skipif(1>0,reason='这个模块不测了,跳过')
def test_01():
    print("--01--")
def test_02():
    print("--02--")
def test_03():
    print("--03--")
class Test_a:
    def test_method(self):
        print("--method--")
"""test_c.py"""
def test_01():
    print("--01--")
"""执行结果"""
Case/test_a.py::test_01 SKIPPED (这个模块不测了,跳过)
Case/test_a.py::test_02 SKIPPED (这个模块不测了,跳过)
Case/test_a.py::test_03 SKIPPED (这个模块不测了,跳过)
Case/test_a.py::Test_a::test_method SKIPPED (这个模块不测了,跳过)
Case/test_c.py::test_01 --01--
PASSED

xfail 预失败标志

pytest.mark.xfail(condition=None, *, reason=None, raises=None, run=True, strict=False) 参数

  • condition (bool or str) -- 将测试功能标记为xfail的条件 (True/False 或A condition string ). 如果是bool,还必须指定 reason
  • reason (str) -- 测试函数标记为xfail的原因。
  • raises (Type[Exception]) -- 异常子类预期由测试函数引发;其他异常将使测试失败。
  • run (bool) -- 是否应实际执行测试功能。如果 False ,函数将始终Xfail且不会执行(如果函数是segfaulting,则很有用)。
  • strict (bool) --

condition 如果 False (默认)功能将在终端输出中显示为 xfailed 如果失败了, xpass 如果它通过。在这两种情况下,这不会导致整个测试套件失败。这对于标记 薄片状的 稍后要处理的测试(随机失败的测试)。

如果 True ,功能将在终端输出中显示为 xfailed 如果它失败了,但是如果它意外地通过了,那么它将 fail 测试套件。这对于标记总是失败的函数特别有用,如果函数意外开始通过,则应该有明确的指示(例如,库的新版本修复了已知的错误)。注意如下示例:

import pytest
@pytest.mark.xfail()
def test_01():
    raise Exception("主动异常")
@pytest.mark.xfail(reason='函数级xfail标志')
def test_02():
    raise Exception("主动异常")
@pytest.mark.xfail(raises=RuntimeError)
def test_03():
    raise RuntimeError("主动异常")
@pytest.mark.xfail(run=False,reason='PASS')
def test_04(self):
    raise Exception("主动异常")
@pytest.mark.xfail(struct=False)
def test_05():
    pass
def test_06():
    pytest.xfail(reason="函数级另一种标志")
@pytest.mark.xfail(1<0,reason='函数级另一种标志')
def test_07():
    pass
"""运行结果"""
Case/test_a.py::test_01 XFAIL
Case/test_a.py::test_02 XFAIL (函数级xfail标志)
Case/test_a.py::test_03 XFAIL
Case/test_a.py::test_04 XFAIL ([NOTRUN] PASS)
Case/test_a.py::test_05 XPASS
Case/test_a.py::test_06 XFAIL (函数级另一种标志)
Case/test_a.py::test_07 PASSED
=================== 1 passed, 5 xfailed, 1 xpassed in 0.18s ===================

上述中的condition ,可以理解为条件判断,如果1>0的时候,则为XPASS,反之如上述所示为PASSED。预失败标志,即使失败了也不会抛出异常,但是会抛出标志。

收录于合集 #Pytest

14

上一篇Pytest参数化详解-下下一篇Pytest-插件介绍与使用


目录
相关文章
|
5天前
|
测试技术 iOS开发
pytest Mark标记测试用例
使用`pytest.mark`进行测试用例分组和筛选,如`@pytest.mark.webtest`。通过`pytest -m`参数执行特定标记的用例,例如`pytest -s test_command_param.py -m webtest`。同时,pytest支持内置的skip、skipif和xfail功能来管理特殊用例:skip始终跳过,skipif条件满足时跳过,xfail则标记预期失败的测试。
5 0
|
10月前
|
测试技术
13-pytest-自定义mark标记
13-pytest-自定义mark标记
|
9月前
|
测试技术 Python
Pytest前后置以及fixture实战部分
Pytest前后置以及fixture实战部分
36 0
|
9月前
|
安全 开发工具 git
[BJDCTF2020]Mark loves cat |变量覆盖(三解)
[BJDCTF2020]Mark loves cat |变量覆盖(三解)
88 0
|
10月前
|
测试技术
14-pytest-标记失败xfail使用
14-pytest-标记失败xfail使用
|
10月前
|
测试技术
21-pytest-severity标记用例优先级
21-pytest-severity标记用例优先级
|
11月前
|
SQL 安全 Dubbo
mark好文章
mark好文章
46 0
|
12月前
pytest 前后置方法
pytest 前后置方法
|
移动开发 测试技术
pytest学习和使用24-如何清空allure报告历史记录?我每次都手动删除,有点Low了~
pytest学习和使用24-如何清空allure报告历史记录?我每次都手动删除,有点Low了~
91 0
pytest学习和使用24-如何清空allure报告历史记录?我每次都手动删除,有点Low了~
|
测试技术
pytest学习和使用11-Pytest如何使用自定义标记mark?
pytest学习和使用11-Pytest如何使用自定义标记mark?
65 0
pytest学习和使用11-Pytest如何使用自定义标记mark?