【pytest】assert断言
4625 ワード
assertブレークスルーを使用するには、次の手順に従います。
assert断言はテスト実行結果が予想に合致するかどうかを判断する重要な方法です!
pytestのassertはunittestのassertEqualなどの断言方法よりも柔軟で、様々な条件式をサポートしています.式が成立すれば(条件は最終的にTrue)、断言が通過し、式が失敗すれば(条件は最終的にFalse)、断言が失敗します.
assertがサポートする式:
# test_moduleName.py
class TestClassName:
""" """
def test_func_name(self):
""" """
#
a = ...
#
x = ''
#
assert x == a #
assert x != a #
assert x is a #
assert x in a # a x
assert x not in a # a x
assert x > a # a
assert x < a # a
assert isinstance(x, dict) # x dict
...
アサーション結果分析:
断言が通過するとpytestは使用例実行結果:PASEDのみを出力します.class TestClassName:
""" """
def test_func_name(self):
""" """
a = 1
b = 2
#
assert a < b
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: E:\PycharmProject\Demo, inifile: pytest.ini
collected 1 item
test_moduleName.py::TestClassName::test_func_name PASSED [100%]
============================== 1 passed in 0.04s ==============================
断言に失敗するとpytestは、断言に失敗した遡及情報を印刷し、断言に失敗した位置や変数値などの情報を表示します.class TestClassName:
""" """
def test_func_name(self):
""" """
a = 1
b = 2
#
assert a == b
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: E:\PycharmProject\Demo, inifile: pytest.ini
collected 1 item
test_moduleName.py::TestClassName::test_func_name FAILED [100%]
================================== FAILURES ===================================
________________________ TestClassName.test_func_name _________________________
self =
def test_func_name(self):
""" """
a = 1
b = 2
> assert a == b
E assert 1 == 2
E -1
E +2
test_moduleName.py:60: AssertionError
============================== 1 failed in 0.48s ==============================
アサーション例外情報:
pytestのassertは一般的な条件式のほか、異常情報も断言できます!
コンテキストマネージャwithを使用してpytestを結合します.raises(xxx)はxxx異常を投げ出すかどうかを断言し、例えばpassを用い、xxx異常情報を投げ出さなければfailedを用いる.
# test_moduleName.py
class TestClassName:
""" """
def test_func_name(self):
""" """
#
a = ...
#
x = ''
#
assert x == a #
assert x != a #
assert x is a #
assert x in a # a x
assert x not in a # a x
assert x > a # a
assert x < a # a
assert isinstance(x, dict) # x dict
...
class TestClassName:
""" """
def test_func_name(self):
""" """
a = 1
b = 2
#
assert a < b
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: E:\PycharmProject\Demo, inifile: pytest.ini
collected 1 item
test_moduleName.py::TestClassName::test_func_name PASSED [100%]
============================== 1 passed in 0.04s ==============================
class TestClassName:
""" """
def test_func_name(self):
""" """
a = 1
b = 2
#
assert a == b
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: E:\PycharmProject\Demo, inifile: pytest.ini
collected 1 item
test_moduleName.py::TestClassName::test_func_name FAILED [100%]
================================== FAILURES ===================================
________________________ TestClassName.test_func_name _________________________
self =
def test_func_name(self):
""" """
a = 1
b = 2
> assert a == b
E assert 1 == 2
E -1
E +2
test_moduleName.py:60: AssertionError
============================== 1 failed in 0.48s ==============================
class TestClassName:
""" """
def test_func_name(self):
""" """
# ZeroDivisionError
with pytest.raises(ZeroDivisionError):
# 0 , ZeroDivisionError;
assert 1 / 0
2.このコードpytestは予想される異常情報を検出していないので、例Failedを使用します.
class TestClassName:
""" """
def test_func_name(self):
""" """
# ZeroDivisionError
with pytest.raises(ZeroDivisionError):
# ZeroDivisionError , ;
assert 5 // 2
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: E:\PycharmProject\Demo, inifile: pytest.ini
collected 1 item
test_moduleName.py::TestClassName::test_func_name FAILED [100%]
================================== FAILURES ===================================
________________________ TestClassName.test_func_name _________________________
self =
def test_func_name(self):
""" """
# ZeroDivisionError
with pytest.raises(ZeroDivisionError):
# 0 , ZeroDivisionError;
> assert 5 // 2
E Failed: DID NOT RAISE
test_moduleName.py:60: Failed
============================== 1 failed in 0.07s ==============================
Process finished with exit code 0