pytest扫盲14--pytest.raises()函数文档及在参数化中处理异常
生活随笔
收集整理的這篇文章主要介紹了
pytest扫盲14--pytest.raises()函数文档及在参数化中处理异常
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
pytest.raises() 函數(shù)文檔如下:
def raises( # noqa: F811
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
*args: Any,
**kwargs: Any
) -> Union["RaisesContext[_E]", _pytest._code.ExceptionInfo[_E]]:
r"""
Assert that a code block/function call raises ``expected_exception``
or raise a failure exception otherwise.
:kwparam match: if specified, a string containing a regular expression,
or a regular expression object, that is tested against the string
representation of the exception using ``re.search``. To match a literal
string that may contain `special characters`__, the pattern can
first be escaped with ``re.escape``.
(This is only used when ``pytest.raises`` is used as a context manager,
and passed through to the function otherwise.
When using ``pytest.raises`` as a function, you can use:
``pytest.raises(Exc, func, match="passed on").match("my pattern")``.)
__ https://docs.python.org/3/library/re.html#regular-expression-syntax # 正則表達(dá)式官方文檔
.. currentmodule:: _pytest._code
Use ``pytest.raises`` as a context manager, which will capture the exception of the given
type:: # 使用 pytest.raises 作為上下文管理器,捕獲給定異常類型.
>>> with raises(ZeroDivisionError):
... 1/0
If the code block does not raise the expected exception (``ZeroDivisionError`` in the example
above), or no exception at all, the check will fail instead. # 代碼中未發(fā)生異常,會檢查失敗
You can also use the keyword argument ``match`` to assert that the
exception matches a text or regex:: # 使用關(guān)鍵字或正則表達(dá)式匹配異常文本
>>> with raises(ValueError, match='must be 0 or None'):
... raise ValueError("value must be 0 or None")
>>> with raises(ValueError, match=r'must be d+$'):
... raise ValueError("value must be 42")
The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the
details of the captured exception:: # 上下文管理器會產(chǎn)生一個(gè)對象 ExceptionInfo,用于檢查異常的詳細(xì)信息
>>> with raises(ValueError) as exc_info:
... raise ValueError("value must be 42")
>>> assert exc_info.type is ValueError # 異常類型,異常類型只能是標(biāo)準(zhǔn)類型 .type
>>> assert exc_info.value.args[0] == "value must be 42" # 異常值,元祖 .value.arg[index]
.. note::
When using ``pytest.raises`` as a context manager, it's worthwhile to
note that normal context manager rules apply and that the exception
raised *must* be the final line in the scope of the context manager.
Lines of code after that, within the scope of the context manager will
not be executed. For example:: # 這里是關(guān)于 pytest.raises 作用范圍的例子,只能在with范圍內(nèi)引發(fā)異常,之后的代碼不會只想。要使用必須跳出 with 作用范圍
>>> value = 15
>>> with raises(ValueError) as exc_info:
... if value > 10:
... raise ValueError("value must be <= 10")
... assert exc_info.type is ValueError # this will not execute
Instead, the following approach must be taken (note the difference in
scope)::
>>> with raises(ValueError) as exc_info:
... if value > 10:
... raise ValueError("value must be <= 10")
...
>>> assert exc_info.type is ValueError
**Using with** ``pytest.mark.parametrize`` # 使用 pytest.mark.parametrize 進(jìn)行參數(shù)化,使某些運(yùn)行引發(fā)異常,其他運(yùn)行正常執(zhí)行
When using :ref:`pytest.mark.parametrize ref`
it is possible to parametrize tests such that
some runs raise an exception and others do not.
See :ref:`parametrizing_conditional_raising` for an example.
**Legacy form** # 這里是直接使用 raises() 調(diào)用參數(shù)的一種用法(不推薦)
It is possible to specify a callable by passing a to-be-called lambda::
>>> raises(ZeroDivisionError, lambda: 1/0)
<ExceptionInfo ...>
or you can specify an arbitrary callable with arguments::
>>> def f(x): return 1/x
...
>>> raises(ZeroDivisionError, f, 0)
<ExceptionInfo ...>
>>> raises(ZeroDivisionError, f, x=0)
<ExceptionInfo ...>
The form above is fully supported but discouraged for new code because the
context manager form is regarded as more readable and less error-prone. # 關(guān)于碎片整理的信息
.. note::
Similar to caught exception objects in Python, explicitly clearing
local references to returned ``ExceptionInfo`` objects can
help the Python interpreter speed up its garbage collection.
Clearing those references breaks a reference cycle
(``ExceptionInfo`` --> caught exception --> frame stack raising
the exception --> current frame stack --> local variables -->
``ExceptionInfo``) which makes Python keep all objects referenced
from that cycle (including all local variables in the current
frame) alive until the next cyclic garbage collection run.
More detailed information can be found in the official Python
documentation for :ref:`the try statement <python:try>`.
"""
利用raise()函數(shù)文檔給出的例子再優(yōu)化下 parametrize 參數(shù)化的例子(注意標(biāo)紅的部分,在參數(shù)化數(shù)據(jù)中直接處理異常):
更多關(guān)于 parametrize 的用法見官方文檔: https://docs.pytest.org/en/latest/example/parametrize.html
# File : test_demo_14.py
# IDE : PyCharm
import pytest
def division(a, b):
return int(a / b)
@pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, pytest.raises(ZeroDivisionError)), (6, 8, 0)], ids=['整除', '被除數(shù)為0', '除數(shù)為0', '非整除'])
def test_2(a, b, c):
'''使用 pytest.raises 接收異常'''
if b == 0:
with c:
assert division(a, b) is not None
else:
assert division(a, b) == c
執(zhí)行后:
此例子旨在說明,在進(jìn)行參數(shù)化時(shí),對于預(yù)期會失敗的用例,在構(gòu)造測試數(shù)據(jù)時(shí),也可以采用 python.raise() 對異常進(jìn)行捕獲,甚至斷言。
總結(jié)
以上是生活随笔為你收集整理的pytest扫盲14--pytest.raises()函数文档及在参数化中处理异常的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。