python - Check if expection is raised with pytest -


this question has answer here:

being quite new exception test handling wondering how check if assertion raised.

a.py

class someerror(exception):     pass  class someclass(object):     def __init__(self):         ...         raise someerror         ... 

test_a.py

from a.a import someclass  def test_some_exception_raised():     ? 

what assertion should there check if someerror raised? using pytest.

in order write assertions raised exceptions, can use pytest.raises context manager this:

a.py

class someerror(exception):     pass  class someclass(object):     def __init__(self):         ...         raise someerror("some errror message")         ... 

test_a.py

from .a import someclass, someerror import pytest  def test_some_exception_raised():     pytest.raises(someerror) excinfo:            obj = someclass()        assert 'some errror message' in str(excinfo.value) 

for more exception assertions, click here


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -