【发布时间】:2018-12-08 21:18:21
【问题描述】:
我想写一个hypothesis.stateful.RuleBasedStateMachine,它断言在某些情况下会引发异常。 pytest 提供 raises 上下文管理器,用于编写有关异常的测试。如果我在hypothesis.stateful.rule 中使用pytest.raises,则会不报告导致测试失败的步骤顺序。
在没有pytest.raises 的情况下重写规则会产生所需的行为:显示步骤顺序。
这里是一些示例代码:
from os import getenv
from pytest import raises
from hypothesis.stateful import RuleBasedStateMachine, rule
SHOW_PROBLEM = getenv('SHOW_PROBLEM') == 'yes'
# A state machine which asserts that an exception is raised in under some condition
class FifthCallShouldRaiseValueError(RuleBasedStateMachine):
def __init__(self):
super().__init__()
self.model = Model()
self.count = 0
if SHOW_PROBLEM:
# This version does NOT report the rule sequence
@rule()
def the_rule(self):
self.count += 1
if self.count > 4:
with raises(ValueError):
self.model.method()
else:
# This version DOES report the rule sequence
@rule()
def the_rule(self):
self.count += 1
if self.count > 4:
try:
self.model.method()
except ValueError: assert True
except : assert False
else : assert False
T = FifthCallShouldRaiseValueError.TestCase
# A model that deliberately fails the test, triggering reporting of
# the sequence of steps which lead to the failure.
class Model:
def __init__(self):
self._count = 0
def method(self):
self._count += 1
if self._count > 4:
# Deliberate mistake: raise wrong exception type
raise TypeError
要观察行为差异,请使用
执行测试SHOW_PROBLEM=yes pytest <...>SHOW_PROBLEM=no pytest <...>
在第二种情况下,输出将显示
state = FifthCallShouldRaiseValueError()
state.the_rule()
state.the_rule()
state.the_rule()
state.the_rule()
state.the_rule()
state.teardown()
在第一种情况下,输出中缺少此步骤序列。这是我们所希望的:在这两种情况下都应该显示序列。
pytest.raises 提升 Failed: DID NOT RAISE <class 'ValueError'> 而手写版本提升 AssertionError。前者在未能引发所需异常时提供更多信息,但不知何故似乎阻止 hypothesis.stateful 报告步骤顺序,这告诉我们如何进入该状态,并且通常是最有趣的部分输出。
除了不使用pytest.raises之外,还可以做些什么来缓解这种情况,即确保打印出步骤顺序?
【问题讨论】: