File tree 5 files changed +59
-0
lines changed
5 files changed +59
-0
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,7 @@ Michael Birtwell
69
69
Michael Droettboom
70
70
Mike Lundy
71
71
Nicolas Delaby
72
+ Omar Kohl
72
73
Pieter Mulder
73
74
Piotr Banaszkiewicz
74
75
Punyashloka Biswal
Original file line number Diff line number Diff line change 18
18
* New Add ability to add global properties in the final xunit output file.
19
19
Thanks `@tareqalayan `_ for the complete PR `#1454 `_).
20
20
21
+ * New ``ExceptionInfo.match() `` method to match a regular expression on the
22
+ string representation of an exception. Closes proposal `#372 `_.
23
+ Thanks `@omarkohl `_ for the complete PR (`#1502 `_) and `@nicoddemus `_ for the
24
+ implementation tips.
21
25
22
26
*
23
27
38
42
.. _@kalekundert : https://github.com/kalekundert
39
43
.. _@tareqalayan : https://github.com/tareqalayan
40
44
.. _@palaviv : https://github.com/palaviv
45
+ .. _@omarkohl : https://github.com/omarkohl
41
46
42
47
.. _#1428 : https://github.com/pytest-dev/pytest/pull/1428
43
48
.. _#1444 : https://github.com/pytest-dev/pytest/pull/1444
44
49
.. _#1441 : https://github.com/pytest-dev/pytest/pull/1441
45
50
.. _#1454 : https://github.com/pytest-dev/pytest/pull/1454
46
51
.. _#1468 : https://github.com/pytest-dev/pytest/pull/1468
47
52
.. _#1474 : https://github.com/pytest-dev/pytest/pull/1474
53
+ .. _#1502 : https://github.com/pytest-dev/pytest/pull/1502
54
+ .. _#372 : https://github.com/pytest-dev/pytest/issues/372
48
55
49
56
50
57
2.9.2.dev1
Original file line number Diff line number Diff line change 1
1
import sys
2
2
from inspect import CO_VARARGS , CO_VARKEYWORDS
3
+ import re
3
4
4
5
import py
5
6
@@ -427,6 +428,19 @@ def __unicode__(self):
427
428
loc = ReprFileLocation (entry .path , entry .lineno + 1 , self .exconly ())
428
429
return unicode (loc )
429
430
431
+ def match (self , regexp ):
432
+ """
433
+ Match the regular expression 'regexp' on the string representation of
434
+ the exception. If it matches then True is returned (so that it is
435
+ possible to write 'assert excinfo.match()'). If it doesn't match an
436
+ AssertionError is raised.
437
+ """
438
+ __tracebackhide__ = True
439
+ if not re .search (regexp , str (self .value )):
440
+ assert 0 , "Pattern '{0!s}' not found in '{1!s}'" .format (
441
+ regexp , self .value )
442
+ return True
443
+
430
444
431
445
class FormattedExcinfo (object ):
432
446
""" presenting information about failing Functions and Generators. """
Original file line number Diff line number Diff line change @@ -110,6 +110,24 @@ exceptions your own code is deliberately raising, whereas using
110
110
like documenting unfixed bugs (where the test describes what "should" happen)
111
111
or bugs in dependencies.
112
112
113
+ If you want to test that a regular expression matches on the string
114
+ representation of an exception (like the ``TestCase.assertRaisesRegexp `` method
115
+ from ``unittest ``) you can use the ``ExceptionInfo.match `` method::
116
+
117
+ import pytest
118
+
119
+ def myfunc():
120
+ raise ValueError("Exception 123 raised")
121
+
122
+ def test_match():
123
+ with pytest.raises(ValueError) as excinfo:
124
+ myfunc()
125
+ excinfo.match(r'.* 123 .*')
126
+
127
+ The regexp parameter of the ``match `` method is matched with the ``re.search ``
128
+ function. So in the above example ``excinfo.match('123') `` would have worked as
129
+ well.
130
+
113
131
114
132
.. _`assertwarns` :
115
133
Original file line number Diff line number Diff line change @@ -323,6 +323,25 @@ def test_codepath_Queue_example():
323
323
assert path .basename .lower () == "queue.py"
324
324
assert path .check ()
325
325
326
+ def test_match_succeeds ():
327
+ with pytest .raises (ZeroDivisionError ) as excinfo :
328
+ 0 / 0
329
+ excinfo .match (r'.*zero.*' )
330
+
331
+ def test_match_raises_error (testdir ):
332
+ testdir .makepyfile ("""
333
+ import pytest
334
+ def test_division_zero():
335
+ with pytest.raises(ZeroDivisionError) as excinfo:
336
+ 0 / 0
337
+ excinfo.match(r'[123]+')
338
+ """ )
339
+ result = testdir .runpytest ()
340
+ assert result .ret != 0
341
+ result .stdout .fnmatch_lines ([
342
+ "*AssertionError*Pattern*[123]*not found*" ,
343
+ ])
344
+
326
345
class TestFormattedExcinfo :
327
346
def pytest_funcarg__importasmod (self , request ):
328
347
def importasmod (source ):
You can’t perform that action at this time.
0 commit comments