Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
unittest.test.test_program.TestCommandLineArgs Class Reference

List of all members.

Public Member Functions

def setUp
def testHelpAndUnknown
def testVerbosity
def testBufferCatchFailfast
def testWarning
def testRunTestsRunnerClass
def testRunTestsRunnerInstance
def testRunTestsOldRunnerClass
def testCatchBreakInstallsHandler
def testParseArgsFileNames
def testParseArgsFilePaths
def testParseArgsNonExistentFiles
def testParseArgsAbsolutePathsThatCanBeConverted
def testParseArgsAbsolutePathsThatCannotBeConverted

Public Attributes

 program
 installed

Private Member Functions

def _patch_isfile

Detailed Description

Definition at line 125 of file test_program.py.


Member Function Documentation

def unittest.test.test_program.TestCommandLineArgs._patch_isfile (   self,
  names,
  exists = True 
) [private]

Definition at line 277 of file test_program.py.

00277 
00278     def _patch_isfile(self, names, exists=True):
00279         def isfile(path):
00280             return path in names
00281         original = os.path.isfile
00282         os.path.isfile = isfile
00283         def restore():
00284             os.path.isfile = original
00285         self.addCleanup(restore)
00286 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 127 of file test_program.py.

00127 
00128     def setUp(self):
00129         self.program = InitialisableProgram()
00130         self.program.createTests = lambda: None
00131         FakeRunner.initArgs = None
00132         FakeRunner.test = None
00133         FakeRunner.raiseError = False

Definition at line 164 of file test_program.py.

00164 
00165     def testBufferCatchFailfast(self):
00166         program = self.program
00167         for arg, attr in (('buffer', 'buffer'), ('failfast', 'failfast'),
00168                       ('catch', 'catchbreak')):
00169             if attr == 'catch' and not hasInstallHandler:
00170                 continue
00171 
00172             short_opt = '-%s' % arg[0]
00173             long_opt = '--%s' % arg
00174             for opt in short_opt, long_opt:
00175                 setattr(program, attr, None)
00176 
00177                 program.parseArgs([None, opt])
00178                 self.assertTrue(getattr(program, attr))
00179 
00180             for opt in short_opt, long_opt:
00181                 not_none = object()
00182                 setattr(program, attr, not_none)
00183 
00184                 program.parseArgs([None, opt])
00185                 self.assertEqual(getattr(program, attr), not_none)

Here is the call graph for this function:

Definition at line 257 of file test_program.py.

00257 
00258     def testCatchBreakInstallsHandler(self):
00259         module = sys.modules['unittest.main']
00260         original = module.installHandler
00261         def restore():
00262             module.installHandler = original
00263         self.addCleanup(restore)
00264 
00265         self.installed = False
00266         def fakeInstallHandler():
00267             self.installed = True
00268         module.installHandler = fakeInstallHandler
00269 
00270         program = self.program
00271         program.catchbreak = True
00272 
00273         program.testRunner = FakeRunner
00274 
00275         program.runTests()
00276         self.assertTrue(self.installed)

Here is the call graph for this function:

Definition at line 134 of file test_program.py.

00134 
00135     def testHelpAndUnknown(self):
00136         program = self.program
00137         def usageExit(msg=None):
00138             program.msg = msg
00139             program.exit = True
00140         program.usageExit = usageExit
00141 
00142         for opt in '-h', '-H', '--help':
00143             program.exit = False
00144             program.parseArgs([None, opt])
00145             self.assertTrue(program.exit)
00146             self.assertIsNone(program.msg)
00147 
00148         program.parseArgs([None, '-$'])
00149         self.assertTrue(program.exit)
00150         self.assertIsNotNone(program.msg)

Here is the call graph for this function:

Definition at line 324 of file test_program.py.

00324 
00325     def testParseArgsAbsolutePathsThatCanBeConverted(self):
00326         cur_dir = os.getcwd()
00327         program = self.program
00328         def _join(name):
00329             return os.path.join(cur_dir, name)
00330         argv = ['progname', _join('foo/bar/baz.py'), _join('green\\red.py')]
00331         self._patch_isfile(argv)
00332 
00333         program.createTests = lambda: None
00334         program.parseArgs(argv)
00335 
00336         expected = ['foo.bar.baz', 'green.red']
00337         self.assertEqual(program.testNames, expected)

Here is the call graph for this function:

Definition at line 338 of file test_program.py.

00338 
00339     def testParseArgsAbsolutePathsThatCannotBeConverted(self):
00340         program = self.program
00341         # even on Windows '/...' is considered absolute by os.path.abspath
00342         argv = ['progname', '/foo/bar/baz.py', '/green/red.py']
00343         self._patch_isfile(argv)
00344 
00345         program.createTests = lambda: None
00346         program.parseArgs(argv)
00347 
00348         self.assertEqual(program.testNames, argv[1:])
00349 
00350         # it may be better to use platform specific functions to normalise paths
00351         # rather than accepting '.PY' and '\' as file seprator on Linux / Mac
00352         # it would also be better to check that a filename is a valid module
00353         # identifier (we have a regex for this in loader.py)
00354         # for invalid filenames should we raise a useful error rather than
00355         # leaving the current error message (import of filename fails) in place?
00356 

Here is the call graph for this function:

Definition at line 287 of file test_program.py.

00287 
00288     def testParseArgsFileNames(self):
00289         # running tests with filenames instead of module names
00290         program = self.program
00291         argv = ['progname', 'foo.py', 'bar.Py', 'baz.PY', 'wing.txt']
00292         self._patch_isfile(argv)
00293 
00294         program.createTests = lambda: None
00295         program.parseArgs(argv)
00296 
00297         # note that 'wing.txt' is not a Python file so the name should
00298         # *not* be converted to a module name
00299         expected = ['foo', 'bar', 'baz', 'wing.txt']
00300         self.assertEqual(program.testNames, expected)
00301 

Here is the call graph for this function:

Definition at line 302 of file test_program.py.

00302 
00303     def testParseArgsFilePaths(self):
00304         program = self.program
00305         argv = ['progname', 'foo/bar/baz.py', 'green\\red.py']
00306         self._patch_isfile(argv)
00307 
00308         program.createTests = lambda: None
00309         program.parseArgs(argv)
00310 
00311         expected = ['foo.bar.baz', 'green.red']
00312         self.assertEqual(program.testNames, expected)
00313 

Here is the call graph for this function:

Definition at line 314 of file test_program.py.

00314 
00315     def testParseArgsNonExistentFiles(self):
00316         program = self.program
00317         argv = ['progname', 'foo/bar/baz.py', 'green\\red.py']
00318         self._patch_isfile([])
00319 
00320         program.createTests = lambda: None
00321         program.parseArgs(argv)
00322 
00323         self.assertEqual(program.testNames, argv[1:])

Here is the call graph for this function:

Definition at line 239 of file test_program.py.

00239 
00240     def testRunTestsOldRunnerClass(self):
00241         program = self.program
00242 
00243         FakeRunner.raiseError = True
00244         program.testRunner = FakeRunner
00245         program.verbosity = 'verbosity'
00246         program.failfast = 'failfast'
00247         program.buffer = 'buffer'
00248         program.test = 'test'
00249 
00250         program.runTests()
00251 
00252         # If initialising raises a type error it should be retried
00253         # without the new keyword arguments
00254         self.assertEqual(FakeRunner.initArgs, {})
00255         self.assertEqual(FakeRunner.test, 'test')
00256         self.assertIs(program.result, RESULT)

Here is the call graph for this function:

Definition at line 207 of file test_program.py.

00207 
00208     def testRunTestsRunnerClass(self):
00209         program = self.program
00210 
00211         program.testRunner = FakeRunner
00212         program.verbosity = 'verbosity'
00213         program.failfast = 'failfast'
00214         program.buffer = 'buffer'
00215         program.warnings = 'warnings'
00216 
00217         program.runTests()
00218 
00219         self.assertEqual(FakeRunner.initArgs, {'verbosity': 'verbosity',
00220                                                 'failfast': 'failfast',
00221                                                 'buffer': 'buffer',
00222                                                 'warnings': 'warnings'})
00223         self.assertEqual(FakeRunner.test, 'test')
00224         self.assertIs(program.result, RESULT)

Here is the call graph for this function:

Definition at line 225 of file test_program.py.

00225 
00226     def testRunTestsRunnerInstance(self):
00227         program = self.program
00228 
00229         program.testRunner = FakeRunner()
00230         FakeRunner.initArgs = None
00231 
00232         program.runTests()
00233 
00234         # A new FakeRunner should not have been instantiated
00235         self.assertIsNone(FakeRunner.initArgs)
00236 
00237         self.assertEqual(FakeRunner.test, 'test')
00238         self.assertIs(program.result, RESULT)

Here is the call graph for this function:

Definition at line 151 of file test_program.py.

00151 
00152     def testVerbosity(self):
00153         program = self.program
00154 
00155         for opt in '-q', '--quiet':
00156             program.verbosity = 1
00157             program.parseArgs([None, opt])
00158             self.assertEqual(program.verbosity, 0)
00159 
00160         for opt in '-v', '--verbose':
00161             program.verbosity = 1
00162             program.parseArgs([None, opt])
00163             self.assertEqual(program.verbosity, 2)

Here is the call graph for this function:

Test the warnings argument

Definition at line 186 of file test_program.py.

00186 
00187     def testWarning(self):
00188         """Test the warnings argument"""
00189         # see #10535
00190         class FakeTP(unittest.TestProgram):
00191             def parseArgs(self, *args, **kw): pass
00192             def runTests(self, *args, **kw): pass
00193         warnoptions = sys.warnoptions[:]
00194         try:
00195             sys.warnoptions[:] = []
00196             # no warn options, no arg -> default
00197             self.assertEqual(FakeTP().warnings, 'default')
00198             # no warn options, w/ arg -> arg value
00199             self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore')
00200             sys.warnoptions[:] = ['somevalue']
00201             # warn options, no arg -> None
00202             # warn options, w/ arg -> arg value
00203             self.assertEqual(FakeTP().warnings, None)
00204             self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore')
00205         finally:
00206             sys.warnoptions[:] = warnoptions

Here is the call graph for this function:


Member Data Documentation

Definition at line 264 of file test_program.py.

Definition at line 128 of file test_program.py.


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