Pytest.raises custom error message#1616
Conversation
CHANGELOG.rst
Outdated
| `@mikofski`_ for the report and `@tomviner`_ for the PR (`#1544`_). | ||
|
|
||
| * | ||
| * pytest.raises accepts a custom message to raise when no exception accord. |
|
Hey @palaviv, may thanks for the PR! I see one small issue with it thought: since it now accepts a def display_warning(*, code, message):
if not isinstance(code, int):
raise TypeError
if not isinstance(code, str):
raise TypeError
def test_display_warning():
pytest.raises(TypeError, display_warning, code=[], message='a warning')
pytest.raises(TypeError, display_warning, code=123, message=[]) So I suggest we accept and handle the @The-Compiler and @palaviv, what do you guys think? |
|
This sounds like a good suggestion. |
|
|
||
| if not args: | ||
| return RaisesContext(expected_exception) | ||
| if "message" in kwargs: |
There was a problem hiding this comment.
Please update the docstring to include this new keyword argument support; make sure to mention it is only valid in the context manager form. 😁
| the actual exception raised. The main attributes of interest are | ||
| ``.type``, ``.value`` and ``.traceback``. | ||
|
|
||
| In the context manager form you may use the keyword argument |
There was a problem hiding this comment.
Thanks!
One last thing, please add a versionadded directive before this paragraph:
.. versionadded:: 2.10
There was a problem hiding this comment.
shouldn't it be after the paragraph?
There was a problem hiding this comment.
Also wouldn't a versionchanged be more appropriate as the function was changed?
There was a problem hiding this comment.
Hmm that's a good question. The other places in pytest documentation which use this feature put it before the paragraph...
You are right, that's how Python's own documentation does it at least. 😁
There was a problem hiding this comment.
Also wouldn't a versionchanged be more appropriate as the function was changed?
Good point. I was thinking along the lines of adding a new parameter, but I think your suggestion makes more sense.
There was a problem hiding this comment.
I think then that I should keep with the project conventions. Consistency is more important.
|
LGTM! @The-Compiler, if you are OK with it feel free to merge it. 😄 |
| else: | ||
| assert False, "Expected pytest.raises.Exception" | ||
|
|
||
| def test_costum_raise_message(self): |
There was a problem hiding this comment.
costum -> custom, but I'll fix it up when merging
|
Thanks! |
This PR change pytest.raises to accept custom made message that will be raised if no exception was raised. For example:
The use case behind this feature is as follows:
As you can see in this example I can't know at which
inputI have failed from the result of the test. The new message will allow me to change the test as follows:Now I can know the exact
inputthe test failed.