Implement ExceptionInfo.match() to match regexp on str(exception)

This implements similar functionality to
unittest.TestCase.assertRegexpMatches()

closes #372
This commit is contained in:
Omar Kohl
2016-04-02 17:24:08 +02:00
parent fed89ef549
commit c578226d43
4 changed files with 58 additions and 0 deletions

View File

@@ -110,6 +110,24 @@ exceptions your own code is deliberately raising, whereas using
like documenting unfixed bugs (where the test describes what "should" happen)
or bugs in dependencies.
If you want to test that a regular expression matches on the string
representation of an exception (like the ``TestCase.assertRaisesRegexp`` method
from ``unittest``) you can use the ``ExceptionInfo.match`` method::
import pytest
def myfunc():
raise ValueError("Exception 123 raised")
def test_match():
with pytest.raises(ValueError) as excinfo:
myfunc()
excinfo.match(r'.* 123 .*')
The regexp parameter of the ``match`` method is matched with the ``re.search``
function. So in the above example ``excinfo.match('123')`` would have worked as
well.
.. _`assertwarns`: