我已阅读有关使用 pytest 创建异常的文档,但不确定如何在我的代码中定义异常。据说 OutOfRangeError 没有定义。任何帮助表示赞赏。
my_roman_module.py:
def to_roman(n):
'''converts integers/arabic numerals to Roman numerals'''
if not (0<n<4000):
raise OutOfRangeError('number out of range (must be between 1-3999)')
result = ''
for numeral, integer in roman_numerals:
while n >= integer:
result += numeral
n -= integer
return result
test_my_roman_module.py:
import pytest
from my_roman_module import to_roman
def test_not_in_range():
'''to_roman should fail with large input'''
with pytest.raises(OutOfRangeError):
to_roman(4000)
相关分类