zl程序教程

您现在的位置是:首页 >  后端

当前栏目

pytest-断言语句

pytest 语句 断言
2023-09-11 14:14:23 时间

pytest允许在测试用例中使用标准的python断言,如下:

# test_case.py中的内容
def foo(x, y):
  return x + y
def test_01():
  assert foo(3, 4) == 8

本例中的函数期望返回一个固定的值。如果该断言失败了,你会看到该函数的返回值:

$ pytest test_assert1.py
============================= test session starts ==============================
platform darwin ‐‐ Python 3.7.6, pytest‐4.2.1, py‐1.7.0, pluggy‐0.8.1
rootdir: /Users/liuke/test/demo, inifile:
collected 1 item
test_case.py F [100%]
=================================== FAILURES ===================================
________________________________ test_function _________________________________
  def test_function():
>     assert foo(3, 4) == 8
E     assert 7 == 8
E      + where 7 = foo()
test_case.py:5: AssertionError
=========================== 1 failed in 0.07 seconds ===========================

pytest支持显示常见的子表达式的值,包括调用,属性,比较以及二元和一元运算符。(参看Demo
of Python failure reports with purest 这允许你使用你习惯的python的在不丢失内省信息的情况下
构造代码。(什么是内省信息?更详细的内部输出信息?) 如果你为断言指定了输出信息,那么不会
输出任何内省信息,而是在traceback中直接输出指定的信息:

assert a % 2 ==0, "value was odd, should be even"

更多断言内省信息请参考Advanced assertion introspection

最后: 下方这份完整的软件测试视频学习教程已经整理上传完成,朋友们如果需要可以自行免费领取 【保证100%免费】
在这里插入图片描述

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!
在这里插入图片描述