Beispiel #1
0
    def __init__(self, module=__name__, defaultTest=None, argv=None,
                    testRunner=None, testLoader=defaultTestLoader,
                    exit=True, verbosity=1, failfast=None, catchbreak=None,
                    buffer=None, stdout=None):
        if module == __name__:
            self.module = None
        elif istext(module):
            self.module = __import__(module)
            for part in module.split('.')[1:]:
                self.module = getattr(self.module, part)
        else:
            self.module = module
        if argv is None:
            argv = sys.argv
        if stdout is None:
            stdout = sys.stdout
        self.stdout = stdout

        self.exit = exit
        self.failfast = failfast
        self.catchbreak = catchbreak
        self.verbosity = verbosity
        self.buffer = buffer
        self.defaultTest = defaultTest
        self.listtests = False
        self.load_list = None
        self.testRunner = testRunner
        self.testLoader = testLoader
        progName = argv[0]
        if progName.endswith('%srun.py' % os.path.sep):
            elements = progName.split(os.path.sep)
            progName = '%s.run' % elements[-2]
        else:
            progName = os.path.basename(argv[0])
        self.progName = progName
        self.parseArgs(argv)
        if self.load_list:
            # TODO: preserve existing suites (like testresources does in
            # OptimisingTestSuite.add, but with a standard protocol).
            # This is needed because the load_tests hook allows arbitrary
            # suites, even if that is rarely used.
            source = open(self.load_list, 'rb')
            try:
                lines = source.readlines()
            finally:
                source.close()
            test_ids = set(line.strip().decode('utf-8') for line in lines)
            self.test = filter_by_ids(self.test, test_ids)
        if not self.listtests:
            self.runTests()
        else:
            runner = self._get_runner()
            if safe_hasattr(runner, 'list'):
                runner.list(self.test)
            else:
                for test in iterate_tests(self.test):
                    self.stdout.write('%s\n' % test.id())
Beispiel #2
0
def text_content(text):
    """Create a Content object from some text.

    This is useful for adding details which are short strings.
    """
    if not istext(text):
        raise TypeError("text_content must be given text, not '%s'." %
                        type(text).__name__)
    return Content(UTF8_TEXT, lambda: [text.encode('utf8')])
Beispiel #3
0
    def __init__(self, module=__name__, defaultTest=None, argv=None,
                    testRunner=None, testLoader=defaultTestLoader,
                    exit=True, verbosity=1, failfast=None, catchbreak=None,
                    buffer=None, stdout=None):
        if module == __name__:
            self.module = None
        elif istext(module):
            self.module = __import__(module)
            for part in module.split('.')[1:]:
                self.module = getattr(self.module, part)
        else:
            self.module = module
        if argv is None:
            argv = sys.argv
        if stdout is None:
            stdout = sys.stdout

        self.exit = exit
        self.failfast = failfast
        self.catchbreak = catchbreak
        self.verbosity = verbosity
        self.buffer = buffer
        self.defaultTest = defaultTest
        self.listtests = False
        self.load_list = None
        self.testRunner = testRunner
        self.testLoader = testLoader
        progName = argv[0]
        if progName.endswith('%srun.py' % os.path.sep):
            elements = progName.split(os.path.sep)
            progName = '%s.run' % elements[-2]
        else:
            progName = os.path.basename(argv[0])
        self.progName = progName
        self.parseArgs(argv)
        if self.load_list:
            # TODO: preserve existing suites (like testresources does in
            # OptimisingTestSuite.add, but with a standard protocol).
            # This is needed because the load_tests hook allows arbitrary
            # suites, even if that is rarely used.
            source = open(self.load_list, 'rb')
            try:
                lines = source.readlines()
            finally:
                source.close()
            test_ids = set(line.strip().decode('utf-8') for line in lines)
            filtered = unittest.TestSuite()
            for test in iterate_tests(self.test):
                if test.id() in test_ids:
                    filtered.addTest(test)
            self.test = filtered
        if not self.listtests:
            self.runTests()
        else:
            for test in iterate_tests(self.test):
                stdout.write('%s\n' % test.id())
Beispiel #4
0
def text_content(text):
    """Create a Content object from some text.

    This is useful for adding details which are short strings.
    """
    if not istext(text):
        raise TypeError(
            "text_content must be given text, not '%s'." % type(text).__name__
        )
    return Content(UTF8_TEXT, lambda: [text.encode('utf8')])
Beispiel #5
0
 def __str__(self):
     difference = self.mismatch.describe()
     if self.verbose:
         # GZ 2011-08-24: Smelly API? Better to take any object and special
         #                case text inside?
         if istext(self.matchee) or _isbytes(self.matchee):
             matchee = text_repr(self.matchee, multiline=False)
         else:
             matchee = repr(self.matchee)
         return "Match failed. Matchee: %s\nMatcher: %s\nDifference: %s\n" % (matchee, self.matcher, difference)
     else:
         return difference
Beispiel #6
0
 def __str__(self):
     difference = self.mismatch.describe()
     if self.verbose:
         # GZ 2011-08-24: Smelly API? Better to take any object and special
         #                case text inside?
         if istext(self.matchee) or _isbytes(self.matchee):
             matchee = text_repr(self.matchee, multiline=False)
         else:
             matchee = repr(self.matchee)
         return (
             'Match failed. Matchee: %s\nMatcher: %s\nDifference: %s\n' %
             (matchee, self.matcher, difference))
     else:
         return difference
Beispiel #7
0
    def __init__(self, exception, value_re=None):
        """Create a MatchesException that will match exc_info's for exception.

        :param exception: Either an exception instance or type.
            If an instance is given, the type and arguments of the exception
            are checked. If a type is given only the type of the exception is
            checked. If a tuple is given, then as with isinstance, any of the
            types in the tuple matching is sufficient to match.
        :param value_re: If 'exception' is a type, and the matchee exception
            is of the right type, then match against this.  If value_re is a
            string, then assume value_re is a regular expression and match
            the str() of the exception against it.  Otherwise, assume value_re
            is a matcher, and match the exception against it.
        """
        Matcher.__init__(self)
        self.expected = exception
        if istext(value_re):
            value_re = AfterPreproccessing(str, MatchesRegex(value_re), False)
        self.value_re = value_re
        self._is_instance = type(self.expected) not in classtypes() + (tuple, )
Beispiel #8
0
    def __init__(self, exception, value_re=None):
        """Create a MatchesException that will match exc_info's for exception.

        :param exception: Either an exception instance or type.
            If an instance is given, the type and arguments of the exception
            are checked. If a type is given only the type of the exception is
            checked. If a tuple is given, then as with isinstance, any of the
            types in the tuple matching is sufficient to match.
        :param value_re: If 'exception' is a type, and the matchee exception
            is of the right type, then match against this.  If value_re is a
            string, then assume value_re is a regular expression and match
            the str() of the exception against it.  Otherwise, assume value_re
            is a matcher, and match the exception against it.
        """
        Matcher.__init__(self)
        self.expected = exception
        if istext(value_re):
            value_re = AfterPreproccessing(str, MatchesRegex(value_re), False)
        self.value_re = value_re
        self._is_instance = type(self.expected) not in classtypes() + (tuple,)
Beispiel #9
0
 def _format(self, thing):
     # Blocks of text with newlines are formatted as triple-quote
     # strings. Everything else is pretty-printed.
     if istext(thing) or _isbytes(thing):
         return text_repr(thing)
     return pformat(thing)
Beispiel #10
0
 def _format(self, thing):
     # Blocks of text with newlines are formatted as triple-quote
     # strings. Everything else is pretty-printed.
     if istext(thing) or _isbytes(thing):
         return text_repr(thing)
     return pformat(thing)
Beispiel #11
0
 def _format(self, thing):
     # Blocks of text with newlines are formatted as triple-quote
     # strings. Everything else is pretty-printed.
     if istext(thing) and '\n' in thing:
         return '"""\\\n%s"""' % (thing,)
     return pformat(thing)