# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2021/11/26
# 文件名称:test_unint.py
# 作用:unittest断言
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import unittest
class TestU(unittest.TestCase):
def setUp(self) -> None:
self.a = 3
self.b = 4
self.c = 3
self.d = 0
def tearDown(self) -> None:
pass
def test_o(self):
self.assertTrue(self.c, msg="结果为False")
def test_t(self):
self.assertEqual(self.a, self.c, msg="a和c不相等")
if __name__ == "__main__":
unittest.main()
断言 | 说明 |
---|---|
assert a | 判断 a为真 |
assert not a | 判断 a不为真 |
assert a in b | 判断 b 包含 a |
assert a == b | 判断 a 等于 b |
assert a != b | 判断 a 不等于 b |
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2021/11/26
# 文件名称:test_assert.py
# 作用:assert断言
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import pytest
class TestU():
def test_f(self):
a = 3
b = 4
s = a + b
return s
def test_func(self):
assert self.test_f() == 7, "计算结果不是7"
if __name__ == "__main__":
pytest.main()
(venv) F:\pytest_study\test_case\test_d>pytest test_assert.py
============================================ test session starts =============================================
platform win32 -- Python 3.7.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: F:\pytest_study\test_case\test_d
plugins: allure-pytest-2.8.12, cov-2.8.1, forked-1.1.3, html-2.0.1, metadata-1.8.0, ordering-0.6, xdist-1.31.0
collected 2 items
test_assert.py .. [100%]
============================================= 2 passed in 0.31s ==============================================
1 / 0
的异常是不是ZeroDivisionError
,其中ZeroDivisionError
是异常类型,用type
从异常信息中获取;division by zero
是异常的值,使用value
从异常信息中获取。 def test_a(self):
with pytest.raises(ZeroDivisionError) as e:
1 / 0
assert e.type == ZeroDivisionError
assert "division by zero" in str(e.value)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。