Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest.raises custom error message #1616

Merged
merged 6 commits into from
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
* Add ``build/`` and ``dist/`` to the default ``--norecursedirs`` list. Thanks
`@mikofski`_ for the report and `@tomviner`_ for the PR (`#1544`_).

*
* pytest.raises accepts a custom message to raise when no exception accord.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accord -> occured

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Thanks `@palaviv`_ for the PR.

.. _@milliams: https://github.com/milliams
.. _@csaftoiu: https://github.com/csaftoiu
Expand Down
14 changes: 10 additions & 4 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,8 +1412,13 @@ def raises(expected_exception, *args, **kwargs):
elif not isclass(expected_exception):
raise TypeError(msg % type(expected_exception))

if "message" in kwargs:
message = kwargs.pop("message")
else:
message = "DID NOT RAISE {0}".format(expected_exception)

if not args:
return RaisesContext(expected_exception)
return RaisesContext(expected_exception, message)
elif isinstance(args[0], str):
code, = args
assert isinstance(code, str)
Expand All @@ -1434,11 +1439,12 @@ def raises(expected_exception, *args, **kwargs):
func(*args[1:], **kwargs)
except expected_exception:
return _pytest._code.ExceptionInfo()
pytest.fail("DID NOT RAISE {0}".format(expected_exception))
pytest.fail(message)

class RaisesContext(object):
def __init__(self, expected_exception):
def __init__(self, expected_exception, message):
self.expected_exception = expected_exception
self.message = message
self.excinfo = None

def __enter__(self):
Expand All @@ -1448,7 +1454,7 @@ def __enter__(self):
def __exit__(self, *tp):
__tracebackhide__ = True
if tp[0] is None:
pytest.fail("DID NOT RAISE")
pytest.fail(self.message)
if sys.version_info < (2, 7):
# py26: on __exit__() exc_value often does not contain the
# exception value.
Expand Down
17 changes: 17 additions & 0 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,20 @@ def test_no_raise_message(self):
pytest.raises(ValueError, int, '0')
except pytest.raises.Exception as e:
assert e.msg == "DID NOT RAISE {0}".format(repr(ValueError))
try:
with pytest.raises(ValueError):
pass
except pytest.raises.Exception as e:
e.msg == "DID NOT RAISE {0}".format(repr(ValueError))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this missing an assert?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


def test_costum_raise_message(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

costum -> custom, but I'll fix it up when merging

message = "TEST_MESSAGE"
try:
pytest.raises(ValueError, int, '0', message=message)
except pytest.raises.Exception as e:
assert e.msg == message
try:
with pytest.raises(ValueError, message=message):
pass
except pytest.raises.Exception as e:
e.msg == message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this missing an assert?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed