-1

I've read through the documentation on creating exceptions with pytest but am unsure on how to define the exception in my code. It is saying that OutOfRangeError is not defined. Any help is appreciated.

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)
mikanim
  • 409
  • 7
  • 21

1 Answers1

0

pytest doesn't create exception. If you have to define your custom exception then subclasss Exception like

Class OutOfRangeError(Exception):
    pass

and then you case raise OutOfRangeError exception, remember to import OutOfRangeError in your test_my_roman_module.py as well.

Mohit Solanki
  • 2,122
  • 12
  • 20