pytest中文文档之编写断言


Posted in Python onSeptember 12, 2019

编写断言

使用assert编写断言

pytest允许你使用python标准的assert表达式写断言;

例如,你可以这样做:

# test_sample.py

def func(x):
 return x + 1


def test_sample():
 assert func(3) == 5

如果这个断言失败,你会看到func(3)实际的返回值:

/d/Personal Files/Python/pytest-chinese-doc/src (5.1.2)
λ pytest test_sample.py
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\Personal Files\Python\pytest-chinese-doc\src, inifile: pytest.ini
collected 1 item

test_sample.py F                         [100%]

====================================================== FAILURES ======================================================= _____________________________________________________ test_sample _____________________________________________________

 def test_sample():
>  assert func(3) == 5
E  assert 4 == 5
E  + where 4 = func(3)

test_sample.py:28: AssertionError
================================================== 1 failed in 0.05s ==================================================

pytest支持显示常见的python子表达式的值,包括:调用、属性、比较、二进制和一元运算符等(参考pytest支持的python失败时报告的演示);

这允许你在没有模版代码参考的情况下,可以使用的python的数据结构,而无须担心丢失自省的问题;

同时,你也可以为断言指定了一条说明信息,用于失败时的情况说明:

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

编写触发期望异常的断言

你可以使用pytest.raises()作为上下文管理器,来编写一个触发期望异常的断言:

import pytest


def myfunc():
 raise ValueError("Exception 123 raised")


def test_match():
 with pytest.raises(ValueError):
  myfunc()

当用例没有返回ValueError或者没有异常返回时,断言判断失败;

如果你希望同时访问异常的属性,可以这样:

import pytest


def myfunc():
 raise ValueError("Exception 123 raised")


def test_match():
 with pytest.raises(ValueError) as excinfo:
  myfunc()
 assert '123' in str(excinfo.value)

其中,excinfo是ExceptionInfo的一个实例,它封装了异常的信息;常用的属性包括:.type、.value和.traceback;

注意:在上下文管理器的作用域中,raises代码必须是最后一行,否则,其后面的代码将不会执行;所以,如果上述例子改成:

def test_match():
 with pytest.raises(ValueError) as excinfo:
  myfunc()
  assert '456' in str(excinfo.value)

则测试将永远成功,因为assert '456' in str(excinfo.value)并不会执行;

你也可以给pytest.raises()传递一个关键字参数match,来测试异常的字符串表示str(excinfo.value)是否符合给定的正则表达式(和unittest中的TestCase.assertRaisesRegexp方法类似):

import pytest


def myfunc():
 raise ValueError("Exception 123 raised")


def test_match():
 with pytest.raises((ValueError, RuntimeError), match=r'.* 123 .*'):
  myfunc()

pytest实际调用的是re.search()方法来做上述检查;并且,pytest.raises()也支持检查多个期望异常(以元组的形式传递参数),我们只需要触发其中任意一个;

pytest.raises还有另外的一种使用形式:

首先,我们来看一下它在源码中的定义:

# _pytest/python_api.py

def raises( # noqa: F811
 expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
 *args: Any,
 match: Optional[Union[str, "Pattern"]] = None,
 **kwargs: Any
) -> Union["RaisesContext[_E]", Optional[_pytest._code.ExceptionInfo[_E]]]:

它接收一个位置参数expected_exception,一组可变参数args,一个关键字参数match和一组关键字参数kwargs;

接着往下看:

# _pytest/python_api.py

 if not args:
  if kwargs:
   msg = "Unexpected keyword arguments passed to pytest.raises: "
   msg += ", ".join(sorted(kwargs))
   msg += "\nUse context-manager form instead?"
   raise TypeError(msg)
  return RaisesContext(expected_exception, message, match)
 else:
  func = args[0]
  if not callable(func):
   raise TypeError(
    "{!r} object (type: {}) must be callable".format(func, type(func))
   )
  try:
   func(*args[1:], **kwargs)
  except expected_exception as e:
   # We just caught the exception - there is a traceback.
   assert e.__traceback__ is not None
   return _pytest._code.ExceptionInfo.from_exc_info(
    (type(e), e, e.__traceback__)
   )
 fail(message)

其中,args如果存在,那么它的第一个参数必须是一个可调用的对象,否则会报TypeError异常;同时,它会把剩余的args参数和所有kwargs参数传递给这个可调用对象,然后检查这个对象执行之后是否触发指定异常;

所以我们有了一种新的写法:

pytest.raises(ZeroDivisionError, lambda x: 1/x, 0)

# 或者

pytest.raises(ZeroDivisionError, lambda x: 1/x, x=0)

这个时候如果你再传递match参数,是不生效的,因为它只有在if not args:的时候生效;

另外,pytest.mark.xfail()也可以接收一个raises参数,来判断用例是否因为一个具体的异常而导致失败:

@pytest.mark.xfail(raises=IndexError)
def test_f():
 f()

如果f()触发一个IndexError异常,则用例标记为xfailed;如果没有,则正常执行f();

注意:如果f()测试成功,用例的结果是xpassed,而不是passed;

pytest.raises适用于检查由代码故意引发的异常;而@pytest.mark.xfail()更适合用于记录一些未修复的Bug;

特殊数据结构比较时的优化

# test_special_compare.py

def test_set_comparison():
 set1 = set('1308')
 set2 = set('8035')
 assert set1 == set2


def test_long_str_comparison():
 str1 = 'show me codes'
 str2 = 'show me money'
 assert str1 == str2


def test_dict_comparison():
 dict1 = {
  'x': 1,
  'y': 2,
 }
 dict2 = {
  'x': 1,
  'y': 1,
 }
 assert dict1 == dict2

上面,我们检查了三种数据结构的比较:集合、字符串和字典;

/d/Personal Files/Python/pytest-chinese-doc/src (5.1.2)
λ pytest test_special_compare.py
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\Personal Files\Python\pytest-chinese-doc\src, inifile: pytest.ini
collected 3 items

test_special_compare.py FFF                      [100%]

====================================================== FAILURES ======================================================= _________________________________________________ test_set_comparison _________________________________________________

 def test_set_comparison():
  set1 = set('1308')
  set2 = set('8035')
>  assert set1 == set2
E  AssertionError: assert {'0', '1', '3', '8'} == {'0', '3', '5', '8'}
E   Extra items in the left set:
E   '1'
E   Extra items in the right set:
E   '5'
E   Use -v to get the full diff

test_special_compare.py:26: AssertionError
______________________________________________ test_long_str_comparison _______________________________________________

 def test_long_str_comparison():
  str1 = 'show me codes'
  str2 = 'show me money'
>  assert str1 == str2
E  AssertionError: assert 'show me codes' == 'show me money'
E   - show me codes
E   ?   ^ ^ ^
E   + show me money
E   ?   ^ ^ ^

test_special_compare.py:32: AssertionError
________________________________________________ test_dict_comparison _________________________________________________

 def test_dict_comparison():
  dict1 = {
   'x': 1,
   'y': 2,
  }
  dict2 = {
   'x': 1,
   'y': 1,
  }
>  assert dict1 == dict2
E  AssertionError: assert {'x': 1, 'y': 2} == {'x': 1, 'y': 1}
E   Omitting 1 identical items, use -vv to show
E   Differing items:
E   {'y': 2} != {'y': 1}
E   Use -v to get the full diff

test_special_compare.py:44: AssertionError
================================================== 3 failed in 0.09s ==================================================

针对一些特殊的数据结构间的比较,pytest对结果的显示做了一些优化:

  • 集合、列表等:标记出第一个不同的元素;
  • 字符串:标记出不同的部分;
  • 字典:标记出不同的条目;

更多例子参考pytest支持的python失败时报告的演示

为失败断言添加自定义的说明

# test_foo_compare.py

class Foo:
 def __init__(self, val):
  self.val = val

 def __eq__(self, other):
  return self.val == other.val
 
 
def test_foo_compare():
 f1 = Foo(1)
 f2 = Foo(2)
 assert f1 == f2

我们定义了一个Foo对象,也复写了它的__eq__()方法,但当我们执行这个用例时:

/d/Personal Files/Python/pytest-chinese-doc/src (5.1.2)
λ pytest test_foo_compare.py
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\Personal Files\Python\pytest-chinese-doc\src, inifile: pytest.ini
collected 1 item

test_foo_compare.py F                       [100%]

====================================================== FAILURES ======================================================= __________________________________________________ test_foo_compare ___________________________________________________

 def test_foo_compare():
  f1 = Foo(1)
  f2 = Foo(2)
>  assert f1 == f2
E  assert <src.test_foo_compare.Foo object at 0x0000020E90C4E978> == <src.test_foo_compare.Foo object at 0x0000020E90C4E630>

test_foo_compare.py:37: AssertionError
================================================== 1 failed in 0.04s ==================================================

并不能直观的看出来失败的原因;

在这种情况下,我们有两种方法来解决:

  • 复写Foo的__repr__()方法:
def __repr__(self):
  return str(self.val)

我们再执行用例:

luyao@NJ-LUYAO-T460 /d/Personal Files/Python/pytest-chinese-doc/src (5.1.2)
λ pytest test_foo_compare.py
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\Personal Files\Python\pytest-chinese-doc\src, inifile: pytest.ini
collected 1 item

test_foo_compare.py F                                              [100%]

====================================================== FAILURES ======================================================= __________________________________________________ test_foo_compare ___________________________________________________

  def test_foo_compare():
    f1 = Foo(1)
    f2 = Foo(2)
>    assert f1 == f2
E    assert 1 == 2

test_foo_compare.py:37: AssertionError
================================================== 1 failed in 0.06s ==================================================

这时,我们能看到失败的原因是因为1 == 2不成立;

至于__str__()和__repr__()的区别,可以参考StackFlow上的这个问题中的回答:https://stackoverflow.com/questions/1436703/difference-between-str-and-repr

  • 使用pytest_assertrepr_compare这个钩子方法添加自定义的失败说明
# conftest.py

from .test_foo_compare import Foo


def pytest_assertrepr_compare(op, left, right):
  if isinstance(left, Foo) and isinstance(right, Foo) and op == "==":
    return [
      "比较两个Foo实例:", # 顶头写概要
      "  值: {} != {}".format(left.val, right.val), # 除了第一个行,其余都可以缩进
    ]

再次执行:

/d/Personal Files/Python/pytest-chinese-doc/src (5.1.2)
λ pytest test_foo_compare.py
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\Personal Files\Python\pytest-chinese-doc\src, inifile: pytest.ini
collected 1 item

test_foo_compare.py F                                              [100%]

====================================================== FAILURES ======================================================= __________________________________________________ test_foo_compare ___________________________________________________

  def test_foo_compare():
    f1 = Foo(1)
    f2 = Foo(2)
>    assert f1 == f2
E    assert 比较两个Foo实例:
E      值: 1 != 2

test_foo_compare.py:37: AssertionError
================================================== 1 failed in 0.05s ==================================================

我们会看到一个更友好的失败说明;

关于断言自省的细节

当断言失败时,pytest为我们提供了非常人性化的失败说明,中间往往夹杂着相应变量的自省信息,这个我们称为断言的自省;

那么,pytest是如何做到这样的:

  • pytest发现测试模块,并引入他们,与此同时,pytest会复写断言语句,添加自省信息;但是,不是测试模块的断言语句并不会被复写;

复写缓存文件

pytest会把被复写的模块存储到本地作为缓存使用,你可以通过在测试用例的根文件夹中的conftest.py里添加如下配置:

import sys

sys.dont_write_bytecode = True

来禁止这种行为;

但是,它并不会妨碍你享受断言自省的好处,只是不会在本地存储.pyc文件了。

去使能断言自省

你可以通过一下两种方法:

  • 在需要去使能模块的docstring中添加PYTEST_DONT_REWRITE字符串;
  • 执行pytest时,添加--assert=plain选项;

我们来看一下去使能后的效果:

/d/Personal Files/Python/pytest-chinese-doc/src (5.1.2)
λ pytest test_foo_compare.py --assert=plain
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\Personal Files\Python\pytest-chinese-doc\src, inifile: pytest.ini
collected 1 item

test_foo_compare.py F                                              [100%]

====================================================== FAILURES ======================================================= __________________________________________________ test_foo_compare ___________________________________________________

  def test_foo_compare():
    f1 = Foo(1)
    f2 = Foo(2)
>    assert f1 == f2
E    AssertionError

test_foo_compare.py:37: AssertionError
================================================== 1 failed in 0.05s ==================================================

断言失败时的信息就非常的不完整了,我们几乎看不出任何有用的Debug信息;

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
SublimeText 2编译python出错的解决方法(The system cannot find the file specified)
Nov 27 Python
Python函数式编程指南(三):迭代器详解
Jun 24 Python
举例讲解Python设计模式编程的代理模式与抽象工厂模式
Jan 16 Python
探究python中open函数的使用
Mar 01 Python
Python学习之用pygal画世界地图实例
Dec 07 Python
Python常见排序操作示例【字典、列表、指定元素等】
Aug 15 Python
Python实现求两个数组交集的方法示例
Feb 23 Python
python中报错&quot;json.decoder.JSONDecodeError: Expecting value:&quot;的解决
Apr 29 Python
pyqt5实现按钮添加背景图片以及背景图片的切换方法
Jun 13 Python
python实现两张图片拼接为一张图片并保存
Jul 16 Python
Python range、enumerate和zip函数用法详解
Sep 11 Python
python 发送get请求接口详解
Nov 17 Python
python中调试或排错的五种方法示例
Sep 12 #Python
详解Python 中sys.stdin.readline()的用法
Sep 12 #Python
Python3将数据保存为txt文件的方法
Sep 12 #Python
Python3 tkinter 实现文件读取及保存功能
Sep 12 #Python
调试Django时打印SQL语句的日志代码实例
Sep 12 #Python
Python socket非阻塞模块应用示例
Sep 12 #Python
Python的条件锁与事件共享详解
Sep 12 #Python
You might like
php上传图片生成缩略图(GD库)
2016/01/06 PHP
PHP strip_tags() 去字符串中的 HTML、XML 以及 PHP 标签的函数
2016/05/22 PHP
laravel批量生成假数据的方法
2019/10/09 PHP
锋利的jQuery 要点归纳(一) jQuery选择器
2010/03/21 Javascript
让ie运行js时提示允许阻止内容运行的解决方法
2010/10/24 Javascript
基于jquery实现图片广告轮换效果代码
2011/07/07 Javascript
40个有创意的jQuery图片和内容滑动及弹出插件收藏集之三
2012/01/03 Javascript
js触发onchange事件的方法说明
2014/03/08 Javascript
JS实现可缩放、拖动、关闭和最小化的浮动窗口完整实例
2015/03/04 Javascript
JavaScript中获取纯正的undefined的方法
2016/03/06 Javascript
通过bootstrap全面学习less
2016/11/09 Javascript
js每隔两秒输出数组中的一项(实例)
2017/05/28 Javascript
Javascript之图片的延迟加载的实例详解
2017/07/24 Javascript
Vue实例中生命周期created和mounted的区别详解
2017/08/25 Javascript
解决在vue项目中,发版之后,背景图片报错,路径不对的问题
2018/03/06 Javascript
Swiper 4.x 使用方法(移动端网站的内容触摸滑动)
2018/05/17 Javascript
vue+VeeValidate 校验范围实例详解(部分校验,全部校验)
2018/10/19 Javascript
浅谈JavaScript_DOM学习篇_图片切换小案例
2019/03/19 Javascript
Nuxt.js实战和配置详解
2019/08/05 Javascript
超详细的5个Shell脚本实例分享(值得收藏)
2019/08/15 Javascript
python用pickle模块实现“增删改查”的简易功能
2017/06/07 Python
python修改文件内容的3种方法详解
2019/11/15 Python
使用Python制作缩放自如的圣诞老人(圣诞树)
2019/12/25 Python
Python利用全连接神经网络求解MNIST问题详解
2020/01/14 Python
最新pycharm安装教程
2020/11/18 Python
python中添加模块导入路径的方法
2021/02/03 Python
html5表单及新增的改良元素详解
2016/06/07 HTML / CSS
HTML5实现直播间评论滚动效果的代码
2020/05/27 HTML / CSS
介绍一下#error预处理
2015/09/25 面试题
银行演讲稿范文
2014/01/03 职场文书
房屋租赁协议书
2014/04/10 职场文书
公司员工离职感言
2015/08/03 职场文书
MySQL优化之如何写出高质量sql语句
2021/05/17 MySQL
动视暴雪取消疫苗禁令 让所有员工返回线下工作
2022/04/03 其他游戏
Golang 实现 WebSockets 之创建 WebSockets
2022/04/24 Golang
Android Studio实现带三角函数对数运算功能的高级计算器
2022/05/20 Java/Android