pytest断言总结

打印 上一主题 下一主题

主题 542|帖子 542|积分 1626

在 pytest 中,可以使用多种方式举行断言,以验证测试用例的预期结果。以下是一些常见的断言方法:
1. 基本的断言

使用 Python 的内置 assert 语句举行断言。
  1. def test_example():
  2.     assert 1 == 1
复制代码
2. 断言相等

使用 pytest 提供的 pytest 对象举行更详细的断言。
  1. def test_equality():
  2.     assert 1 == 1
  3.     assert 1 != 2
复制代码
3. 断言包含

查抄一个对象是否包含另一个对象。
  1. def test_inclusion():
  2.     assert 'hello' in 'hello world'
  3.     assert 'goodbye' not in 'hello world'
复制代码
4. 断言真假

查抄表达式的真值。
  1. def test_truthiness():
  2.     assert True
  3.     assert not False
复制代码
5. 断言非常

使用 pytest.raises 作为上下文管理器来断言代码块是否抛出非常。
  1. def test_exception():
  2.     with pytest.raises(ZeroDivisionError):
  3.         1 / 0
复制代码
6. 断言警告

使用 pytest.warns 来断言代码块是否发出特定的警告。
  1. import warnings
  2. def test_warning():
  3.     with pytest.warns(UserWarning):
  4.         warnings.warn("This is a warning", UserWarning)
复制代码
7. 断言近似相等

对于浮点数,可以使用 pytest.approx 来断言两个数值近似相等。
  1. def test_approx():
  2.     assert 2.2 == pytest.approx(2.3)
  3.     assert 2.0 == pytest.approx(2.0)
复制代码
8. 断言集合

查抄集合是否相等。
  1. def test_set_equality():
  2.     assert set([1, 2, 3]) == set([3, 2, 1])
复制代码
9. 断言字典

查抄字典是否相等。
  1. def test_dict_equality():
  2.     assert {'a': 1, 'b': 2} == {'b': 2, 'a': 1}
复制代码
10. 断言回调函数

使用 pytest 提供的 pytest_assertrepr_compare 和 pytest.register_assert_rewrite 举行更复杂的断言。
  1. def test_callback():
  2.     assert some_custom_condition(value)
复制代码
11. 断言响应结构

对于更复杂的数据结构,可以使用 pytest 提供的 pytest 对象的内置方法,如 pytest.raises、pytest.warns 等。
12. 断言字符串匹配

使用 pytest 的 re 模块举行正则表达式匹配。
  1. import re
  2. def test_regex_match():
  3.     assert re.search(r"foo", "foobar")
复制代码
13. 断言列表长度

验证列表或集合的长度是否符合预期。
  1. def test_list_length():
  2.     assert len([1, 2, 3]) == 3
复制代码
14. 断言多值

使用 pytest.mark.parametrize 举行参数化测试,并在每个参数组合中应用断言。
  1. @pytest.mark.parametrize("a,b,expected", [(1, 2, 3), (2, 3, 5)])
  2. def test_addition(a, b, expected):
  3.     assert a + b == expected
复制代码
15. 断言调用

使用 pytest 的 mock 对象来断言某个对象的方法是否被调用。
  1. from unittest.mock import Mock
  2. def test_mock_call():
  3.     mock_obj = Mock()
  4.     mock_obj.some_method()
  5.     mock_obj.some_method.assert_called_once()
复制代码
16. 断言属性

查抄对象的属性值。
  1. def test_attribute():
  2.     class MyClass:
  3.         def __init__(self):
  4.             self.my_attr = 42
  5.     obj = MyClass()
  6.     assert obj.my_attr == 42
复制代码
17. 断言文件内容

验证文件内容是否符合预期。
  1. def test_file_content():
  2.     with open("test_file.txt", "r") as f:
  3.         content = f.read()
  4.     assert "expected content" in content
复制代码
18. 断言非常消息

验证非常消息是否符合预期。
  1. def test_exception_message():
  2.     try:
  3.         1 / 0
  4.     except ZeroDivisionError as e:
  5.         assert str(e) == "division by zero"
复制代码
19. 断言浮点数精度

使用 pytest.approx 并指定精度。
  1. def test_float_precision():
  2.     assert 1.1 == pytest.approx(1.10001, abs=1e-5)
复制代码
20. 断言迭代器

查抄迭代器是否产生预期的元素。
  1. def test_iterator():
  2.     assert list(iter([1, 2, 3])) == [1, 2, 3]
复制代码
21. 断言多态性

验证对象是否是特定类型或是否实现了特定的接口。
  1. def test_type():
  2.     assert isinstance(1, int)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

曂沅仴駦

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表