通过AST重写进行断言内省
项目描述
概述
甜点是一个实用库,使Python代码能够通过assert语句引发的断言进行内省。
它是来自pytest的内省代码的独立版本,所有荣誉都应归功于Holger Krekel和pytest的开发者。
用法
使用甜点相当简单
>>> with open(tmp_filename, "w") as f:
... _ = f.write("""
... def func():
... def x():
... return 1
... def y():
... return -1
... assert y() == x()
... """)
>>> import emport
>>> import dessert
>>> with dessert.rewrite_assertions_context():
... module = emport.import_file(tmp_filename)
>>> module.func() # doctest: +IGNORE_EXCEPTION_DETAIL +ELLIPSIS
Traceback (most recent call last):
...
assert y() == x()
AssertionError: assert -1 == 1
+ where -1 = <function y at ...>()
+ and 1 = <function x at ...>()
许可证
甜点在MIT许可证下发布。它有99%基于MIT许可证发布的pytest。