1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

import sys 

import types 

import traceback 

import unittest 

 

 

class TestRunner(unittest.TextTestRunner): 

 

    def __init__(self, stream=sys.stdout): 

        self.stream = unittest._WritelnDecorator(stream) 

 

    def _makeResult(self): 

        return TextTestResult(stream=self.stream) 

 

 

class TextTestResult(unittest.TestResult): 

 

    separator2 = '=' * 70 

 

    def __init__(self, stream): 

        unittest.TestResult.__init__(self) 

        self.stream = stream 

 

    def printErrors(self): 

        self.printErrorList(self.errors) 

        self.printErrorList(self.failures) 

 

    def printErrorList(self, errors): 

        for test, error in errors: 

            self.stream.writeln(error) 

 

    def _exc_info_to_string(self, err, test): 

        """Converts a sys.exc_info()-style tuple of values into a string.""" 

        exctype, value, tb = err 

        # Skip test runner traceback levels 

        while tb and self._is_relevant_tb_level(tb): 

            tb = tb.tb_next 

 

        f = tb.tb_frame 

        lineno = tb.tb_lineno 

        co = f.f_code 

        filename = co.co_filename 

        return '%s:%s:%s' % (filename, lineno, format_exception_only(exctype, 

                                                                     value)) 

 

 

# from unittest.py 

 

class TestProgram(unittest.TestProgram): 

 

    USAGE = """\ 

Usage: %(progName)s [options] [test] [...] 

 

Options: 

  -h, --help        Show this message 

  -v, --verbose     Verbose output 

  -q, --quiet       Minimal output 

  -g, --gnu-format-error 

                    Format error as described in GNU Coding Standards 

 

Examples: 

  %(progName)s                               - run default set of tests 

  %(progName)s MyTestSuite                   - run suite 'MyTestSuite' 

  %(progName)s MyTestCase.testSomething      - run MyTestCase.testSomething 

  %(progName)s MyTestCase                    - run all 'test*' test methods 

                                               in MyTestCase 

""" 

 

    def parseArgs(self, argv): 

        import getopt 

        try: 

            options, args = getopt.getopt(argv[1:], 'hHvqg', 

                                          ['help', 'verbose', 'quiet', 

                                           'gnu-format-error']) 

            for opt, value in options: 

                if opt in ('-h', '-H', '--help'): 

                    self.usageExit() 

                if opt in ('-q', '--quiet'): 

                    self.verbosity = 0 

                if opt in ('-v', '--verbose'): 

                    self.verbosity = 2 

                if opt in ('-g', '--gnu-format-error'): 

                    self.testRunner = TestRunner() 

            if len(args) == 0 and self.defaultTest is None: 

                self.test = self.testLoader.loadTestsFromModule(self.module) 

                return 

            if len(args) > 0: 

                self.testNames = args 

            else: 

                self.testNames = (self.defaultTest,) 

            self.createTests() 

        except getopt.error, msg: 

            self.usageExit(msg) 

 

 

main = TestProgram 

 

 

# taken from traceback module 

 

def format_exception_only(etype, value): 

    """Format the exception part of a traceback. 

 

    The arguments are the exception type and value such as given by 

    sys.last_type and sys.last_value. 

    """ 

 

    # An instance should not have a meaningful value parameter, but 

    # sometimes does, particularly for string exceptions, such as 

    # >>> raise string1, string2  # deprecated 

    # 

    # Clear these out first because issubtype(string1, SyntaxError) 

    # would throw another exception and mask the original problem. 

    if (isinstance(etype, BaseException) or 

        isinstance(etype, types.InstanceType) or 

        etype is None or type(etype) is str): 

        return _format_final_exc_line(etype, value) 

 

    stype = etype.__name__ 

 

    if not issubclass(etype, SyntaxError): 

        return _format_final_exc_line(stype, value) 

 

    # It was a syntax error; show exactly where the problem was found. 

    try: 

        msg, (filename, lineno, offset, badline) = value 

    except Exception: 

        pass 

    else: 

        lines = [] 

        if offset is not None: 

            lines.append('%s:' % offset) 

        value = msg 

 

    lines.append(_format_final_exc_line(stype, value)) 

    return ''.join(lines) 

 

 

def _format_final_exc_line(etype, value): 

    """Return a list of a single line -- 

    normal case for format_exception_only""" 

    valuestr = traceback._some_str(value) 

    if value is None or not valuestr: 

        line = ' %s' % etype 

    else: 

        line = " %s: %s" % (etype, valuestr) 

    return line