我想写一个hypothesis.stateful.RuleBasedStateMachine
断言在某些情况下引发异常的语句。为编写有关异常的测试pytest
提供上下文管理器。raises
如果我pytest.raises
在 a中使用,则不会报告hypothesis.stateful.rule
导致测试失败的步骤序列。
重写规则而不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
?