Fork me on GitHub

pytest-断言语句

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

posted @   流柯  阅读(112)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2015-03-03 jenkins 邮件配置
访客: 浏览:
点击右上角即可分享
微信分享提示
北京
15:07发布
北京
15:07发布
7°
东南风
3级
空气质量
相对湿度
35%
今天
3°/11°
周六
雨夹雪
-2°/6°
周日
晴 / 多云
-1°/8°