Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
unittest.main.TestProgram Class Reference
Inheritance diagram for unittest.main.TestProgram:
Inheritance graph
[legend]
Collaboration diagram for unittest.main.TestProgram:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def usageExit
def parseArgs
def createTests
def runTests

Public Attributes

 module
 exit
 catchbreak
 verbosity
 buffer
 warnings
 defaultTest
 testRunner
 testLoader
 progName
 testNames
 test
 result
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Static Public Attributes

 USAGE = USAGE_FROM_MODULE
 failfast = catchbreakbufferprogNamewarningsNone

Private Member Functions

def _do_discovery

Detailed Description

A command-line program that runs a set of tests; this is primarily
   for making test modules conveniently executable.

Definition at line 80 of file main.py.


Constructor & Destructor Documentation

def unittest.main.TestProgram.__init__ (   self,
  module = '__main__',
  defaultTest = None,
  argv = None,
  testRunner = None,
  testLoader = loader.defaultTestLoader,
  exit = True,
  verbosity = 1,
  failfast = None,
  catchbreak = None,
  buffer = None,
  warnings = None 
)

Definition at line 92 of file main.py.

00092 
00093                     buffer=None, warnings=None):
00094         if isinstance(module, str):
00095             self.module = __import__(module)
00096             for part in module.split('.')[1:]:
00097                 self.module = getattr(self.module, part)
00098         else:
00099             self.module = module
00100         if argv is None:
00101             argv = sys.argv
00102 
00103         self.exit = exit
00104         self.failfast = failfast
00105         self.catchbreak = catchbreak
00106         self.verbosity = verbosity
00107         self.buffer = buffer
00108         if warnings is None and not sys.warnoptions:
00109             # even if DreprecationWarnings are ignored by default
00110             # print them anyway unless other warnings settings are
00111             # specified by the warnings arg or the -W python flag
00112             self.warnings = 'default'
00113         else:
00114             # here self.warnings is set either to the value passed
00115             # to the warnings args or to None.
00116             # If the user didn't pass a value self.warnings will
00117             # be None. This means that the behavior is unchanged
00118             # and depends on the values passed to -W.
00119             self.warnings = warnings
00120         self.defaultTest = defaultTest
00121         self.testRunner = testRunner
00122         self.testLoader = testLoader
00123         self.progName = os.path.basename(argv[0])
00124         self.parseArgs(argv)
00125         self.runTests()

Here is the caller graph for this function:


Member Function Documentation

def unittest.main.TestProgram._do_discovery (   self,
  argv,
  Loader = loader.TestLoader 
) [private]

Definition at line 200 of file main.py.

00200 
00201     def _do_discovery(self, argv, Loader=loader.TestLoader):
00202         # handle command line args for test discovery
00203         self.progName = '%s discover' % self.progName
00204         import optparse
00205         parser = optparse.OptionParser()
00206         parser.prog = self.progName
00207         parser.add_option('-v', '--verbose', dest='verbose', default=False,
00208                           help='Verbose output', action='store_true')
00209         if self.failfast != False:
00210             parser.add_option('-f', '--failfast', dest='failfast', default=False,
00211                               help='Stop on first fail or error',
00212                               action='store_true')
00213         if self.catchbreak != False:
00214             parser.add_option('-c', '--catch', dest='catchbreak', default=False,
00215                               help='Catch ctrl-C and display results so far',
00216                               action='store_true')
00217         if self.buffer != False:
00218             parser.add_option('-b', '--buffer', dest='buffer', default=False,
00219                               help='Buffer stdout and stderr during tests',
00220                               action='store_true')
00221         parser.add_option('-s', '--start-directory', dest='start', default='.',
00222                           help="Directory to start discovery ('.' default)")
00223         parser.add_option('-p', '--pattern', dest='pattern', default='test*.py',
00224                           help="Pattern to match tests ('test*.py' default)")
00225         parser.add_option('-t', '--top-level-directory', dest='top', default=None,
00226                           help='Top level directory of project (defaults to start directory)')
00227 
00228         options, args = parser.parse_args(argv)
00229         if len(args) > 3:
00230             self.usageExit()
00231 
00232         for name, value in zip(('start', 'pattern', 'top'), args):
00233             setattr(options, name, value)
00234 
00235         # only set options from the parsing here
00236         # if they weren't set explicitly in the constructor
00237         if self.failfast is None:
00238             self.failfast = options.failfast
00239         if self.catchbreak is None:
00240             self.catchbreak = options.catchbreak
00241         if self.buffer is None:
00242             self.buffer = options.buffer
00243 
00244         if options.verbose:
00245             self.verbosity = 2
00246 
00247         start_dir = options.start
00248         pattern = options.pattern
00249         top_level_dir = options.top
00250 
00251         loader = Loader()
00252         self.test = loader.discover(start_dir, pattern, top_level_dir)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 193 of file main.py.

00193 
00194     def createTests(self):
00195         if self.testNames is None:
00196             self.test = self.testLoader.loadTestsFromModule(self.module)
00197         else:
00198             self.test = self.testLoader.loadTestsFromNames(self.testNames,
00199                                                            self.module)

def unittest.main.TestProgram.parseArgs (   self,
  argv 
)

Definition at line 140 of file main.py.

00140 
00141     def parseArgs(self, argv):
00142         if ((len(argv) > 1 and argv[1].lower() == 'discover') or
00143             (len(argv) == 1 and self.module is None)):
00144             self._do_discovery(argv[2:])
00145             return
00146 
00147         import getopt
00148         long_opts = ['help', 'verbose', 'quiet', 'failfast', 'catch', 'buffer']
00149         try:
00150             options, args = getopt.getopt(argv[1:], 'hHvqfcb', long_opts)
00151         except getopt.error as msg:
00152             self.usageExit(msg)
00153             return
00154 
00155         for opt, value in options:
00156             if opt in ('-h','-H','--help'):
00157                 self.usageExit()
00158             if opt in ('-q','--quiet'):
00159                 self.verbosity = 0
00160             if opt in ('-v','--verbose'):
00161                 self.verbosity = 2
00162             if opt in ('-f','--failfast'):
00163                 if self.failfast is None:
00164                     self.failfast = True
00165                 # Should this raise an exception if -f is not valid?
00166             if opt in ('-c','--catch'):
00167                 if self.catchbreak is None:
00168                     self.catchbreak = True
00169                 # Should this raise an exception if -c is not valid?
00170             if opt in ('-b','--buffer'):
00171                 if self.buffer is None:
00172                     self.buffer = True
00173                 # Should this raise an exception if -b is not valid?
00174 
00175         if len(args) == 0 and self.module is None:
00176             # this allows "python -m unittest -v" to still work for
00177             # test discovery. This means -c / -b / -v / -f options will
00178             # be handled twice, which is harmless but not ideal.
00179             self._do_discovery(argv[1:])
00180             return
00181 
00182         if len(args) == 0 and self.defaultTest is None:
00183             # createTests will load tests from self.module
00184             self.testNames = None
00185         elif len(args) > 0:
00186             self.testNames = _convert_names(args)
00187             if __name__ == '__main__':
00188                 # to support python -m unittest ...
00189                 self.module = None
00190         else:
00191             self.testNames = (self.defaultTest,)
00192         self.createTests()

Here is the call graph for this function:

Definition at line 253 of file main.py.

00253 
00254     def runTests(self):
00255         if self.catchbreak:
00256             installHandler()
00257         if self.testRunner is None:
00258             self.testRunner = runner.TextTestRunner
00259         if isinstance(self.testRunner, type):
00260             try:
00261                 testRunner = self.testRunner(verbosity=self.verbosity,
00262                                              failfast=self.failfast,
00263                                              buffer=self.buffer,
00264                                              warnings=self.warnings)
00265             except TypeError:
00266                 # didn't accept the verbosity, buffer or failfast arguments
00267                 testRunner = self.testRunner()
00268         else:
00269             # it is assumed to be a TestRunner instance
00270             testRunner = self.testRunner
00271         self.result = testRunner.run(self.test)
00272         if self.exit:
00273             sys.exit(not self.result.wasSuccessful())

Here is the call graph for this function:

def unittest.main.TestProgram.usageExit (   self,
  msg = None 
)

Definition at line 126 of file main.py.

00126 
00127     def usageExit(self, msg=None):
00128         if msg:
00129             print(msg)
00130         usage = {'progName': self.progName, 'catchbreak': '', 'failfast': '',
00131                  'buffer': ''}
00132         if self.failfast != False:
00133             usage['failfast'] = FAILFAST
00134         if self.catchbreak != False:
00135             usage['catchbreak'] = CATCHBREAK
00136         if self.buffer != False:
00137             usage['buffer'] = BUFFEROUTPUT
00138         print(self.USAGE % usage)
00139         sys.exit(2)

Here is the caller graph for this function:


Member Data Documentation

Definition at line 106 of file main.py.

Definition at line 104 of file main.py.

Definition at line 119 of file main.py.

Definition at line 102 of file main.py.

unittest.main.TestProgram.failfast = catchbreakbufferprogNamewarningsNone [static]

Definition at line 87 of file main.py.

Definition at line 94 of file main.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

Definition at line 122 of file main.py.

Definition at line 270 of file main.py.

Definition at line 195 of file main.py.

Definition at line 121 of file main.py.

Definition at line 183 of file main.py.

Definition at line 120 of file main.py.

Definition at line 84 of file main.py.

Definition at line 105 of file main.py.

Definition at line 111 of file main.py.


The documentation for this class was generated from the following file: