~jml/testtools/better-doctest-output-checker

« back to all changes in this revision

Viewing changes to testtools/testcase.py

ExpectedException now more useful.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import copy
18
18
import itertools
19
 
import re
20
19
import sys
21
20
import types
22
21
import unittest
29
28
from testtools.matchers import (
30
29
    Annotate,
31
30
    Equals,
 
31
    MatchesException,
32
32
    Is,
33
33
    Not,
34
34
    )
754
754
    exception is raised, an AssertionError will be raised.
755
755
    """
756
756
 
757
 
    def __init__(self, exc_type, value_re):
 
757
    def __init__(self, exc_type, value_re=None):
758
758
        """Construct an `ExpectedException`.
759
759
 
760
760
        :param exc_type: The type of exception to expect.
772
772
            raise AssertionError('%s not raised.' % self.exc_type.__name__)
773
773
        if exc_type != self.exc_type:
774
774
            return False
775
 
        if not re.match(self.value_re, str(exc_value)):
776
 
            raise AssertionError('"%s" does not match "%s".' %
777
 
                                 (str(exc_value), self.value_re))
 
775
        if self.value_re:
 
776
            matcher = MatchesException(self.exc_type, self.value_re)
 
777
            mismatch = matcher.match((exc_type, exc_value, traceback))
 
778
            if mismatch:
 
779
                raise AssertionError(mismatch.describe())
778
780
        return True
779
781
 
780
782