【发布时间】:2016-11-18 17:23:51
【问题描述】:
我正在研究如何在 Python 中执行断言自省,方法与 that py.test does 相同。比如……
>>> a = 1
>>> b = 2
>>> assert a == b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError # <--- I want more information here, eg 'AssertionError: 1 != 2'
我看到py.code 库has some functionality around this 和我还看到this answer,注意到sys.excepthook 允许你插入任何你想要异常的行为,但我不清楚如何放置这一切都在一起。
【问题讨论】:
-
为什么不使用
pytest本身? -
@pylang 我可以重新使用 py.test 允许这种情况发生的部分,但我不能只使用 py.test 本身。出于我的问题的目的,我说你应该假设例如。我实际上并没有在这种情况下运行测试。
-
@pylang 实际上可以在一般上下文中使用 pytest 的断言吗?我假设 OP 希望在独立程序/库中使用该功能,例如,用于验证目的。
-
我想我现在明白了。是的,我不相信 pytes 有交互模式(至少在 jupyter 笔记本中没有)。传统上,由于这个确切的问题,我使用了
nose。