2010-05-07 12:35:24 -03:00
|
|
|
import os
|
2010-05-07 13:00:30 -03:00
|
|
|
import sys
|
2017-11-25 11:23:52 -04:00
|
|
|
import subprocess
|
2013-08-29 06:37:28 -03:00
|
|
|
from test import support
|
2010-03-27 09:34:21 -03:00
|
|
|
import unittest
|
2022-06-21 05:27:59 -03:00
|
|
|
import test.test_unittest
|
|
|
|
from test.test_unittest.test_result import BufferedWriter
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
|
|
|
|
class Test_TestProgram(unittest.TestCase):
|
|
|
|
|
2010-05-07 12:35:24 -03:00
|
|
|
def test_discovery_from_dotted_path(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
tests = [self]
|
2022-06-21 05:27:59 -03:00
|
|
|
expectedPath = os.path.abspath(os.path.dirname(test.test_unittest.__file__))
|
2010-05-07 12:35:24 -03:00
|
|
|
|
|
|
|
self.wasRun = False
|
|
|
|
def _find_tests(start_dir, pattern):
|
|
|
|
self.wasRun = True
|
|
|
|
self.assertEqual(start_dir, expectedPath)
|
|
|
|
return tests
|
|
|
|
loader._find_tests = _find_tests
|
2022-06-21 05:27:59 -03:00
|
|
|
suite = loader.discover('test.test_unittest')
|
2010-05-07 12:35:24 -03:00
|
|
|
self.assertTrue(self.wasRun)
|
|
|
|
self.assertEqual(suite._tests, tests)
|
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
# Horrible white box test
|
|
|
|
def testNoExit(self):
|
|
|
|
result = object()
|
|
|
|
test = object()
|
|
|
|
|
|
|
|
class FakeRunner(object):
|
|
|
|
def run(self, test):
|
|
|
|
self.test = test
|
|
|
|
return result
|
|
|
|
|
|
|
|
runner = FakeRunner()
|
|
|
|
|
|
|
|
oldParseArgs = unittest.TestProgram.parseArgs
|
|
|
|
def restoreParseArgs():
|
|
|
|
unittest.TestProgram.parseArgs = oldParseArgs
|
|
|
|
unittest.TestProgram.parseArgs = lambda *args: None
|
|
|
|
self.addCleanup(restoreParseArgs)
|
|
|
|
|
|
|
|
def removeTest():
|
|
|
|
del unittest.TestProgram.test
|
|
|
|
unittest.TestProgram.test = test
|
|
|
|
self.addCleanup(removeTest)
|
|
|
|
|
|
|
|
program = unittest.TestProgram(testRunner=runner, exit=False, verbosity=2)
|
|
|
|
|
|
|
|
self.assertEqual(program.result, result)
|
|
|
|
self.assertEqual(runner.test, test)
|
|
|
|
self.assertEqual(program.verbosity, 2)
|
|
|
|
|
|
|
|
class FooBar(unittest.TestCase):
|
|
|
|
def testPass(self):
|
2021-12-17 05:10:55 -04:00
|
|
|
pass
|
2010-03-27 09:34:21 -03:00
|
|
|
def testFail(self):
|
2021-12-17 05:10:55 -04:00
|
|
|
raise AssertionError
|
2021-12-26 07:22:44 -04:00
|
|
|
def testError(self):
|
|
|
|
1/0
|
|
|
|
@unittest.skip('skipping')
|
|
|
|
def testSkipped(self):
|
|
|
|
raise AssertionError
|
|
|
|
@unittest.expectedFailure
|
|
|
|
def testExpectedFailure(self):
|
|
|
|
raise AssertionError
|
|
|
|
@unittest.expectedFailure
|
|
|
|
def testUnexpectedSuccess(self):
|
|
|
|
pass
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2023-04-26 22:28:46 -03:00
|
|
|
class Empty(unittest.TestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TestLoader(unittest.TestLoader):
|
|
|
|
"""Test loader that returns a suite containing the supplied testcase."""
|
|
|
|
|
|
|
|
def __init__(self, testcase):
|
|
|
|
self.testcase = testcase
|
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
def loadTestsFromModule(self, module):
|
|
|
|
return self.suiteClass(
|
2023-04-26 22:28:46 -03:00
|
|
|
[self.loadTestsFromTestCase(self.testcase)])
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2013-02-23 13:52:51 -04:00
|
|
|
def loadTestsFromNames(self, names, module):
|
|
|
|
return self.suiteClass(
|
2023-04-26 22:28:46 -03:00
|
|
|
[self.loadTestsFromTestCase(self.testcase)])
|
2013-02-23 13:52:51 -04:00
|
|
|
|
|
|
|
def test_defaultTest_with_string(self):
|
|
|
|
class FakeRunner(object):
|
|
|
|
def run(self, test):
|
|
|
|
self.test = test
|
|
|
|
return True
|
|
|
|
|
|
|
|
old_argv = sys.argv
|
|
|
|
sys.argv = ['faketest']
|
|
|
|
runner = FakeRunner()
|
|
|
|
program = unittest.TestProgram(testRunner=runner, exit=False,
|
2022-06-21 05:27:59 -03:00
|
|
|
defaultTest='test.test_unittest',
|
2023-04-26 22:28:46 -03:00
|
|
|
testLoader=self.TestLoader(self.FooBar))
|
2013-02-23 13:52:51 -04:00
|
|
|
sys.argv = old_argv
|
2022-06-21 05:27:59 -03:00
|
|
|
self.assertEqual(('test.test_unittest',), program.testNames)
|
2013-02-23 13:52:51 -04:00
|
|
|
|
|
|
|
def test_defaultTest_with_iterable(self):
|
|
|
|
class FakeRunner(object):
|
|
|
|
def run(self, test):
|
|
|
|
self.test = test
|
|
|
|
return True
|
|
|
|
|
|
|
|
old_argv = sys.argv
|
|
|
|
sys.argv = ['faketest']
|
|
|
|
runner = FakeRunner()
|
|
|
|
program = unittest.TestProgram(
|
|
|
|
testRunner=runner, exit=False,
|
2022-06-21 05:27:59 -03:00
|
|
|
defaultTest=['test.test_unittest', 'test.test_unittest2'],
|
2023-04-26 22:28:46 -03:00
|
|
|
testLoader=self.TestLoader(self.FooBar))
|
2013-02-23 13:52:51 -04:00
|
|
|
sys.argv = old_argv
|
2022-06-21 05:27:59 -03:00
|
|
|
self.assertEqual(['test.test_unittest', 'test.test_unittest2'],
|
2013-02-23 13:52:51 -04:00
|
|
|
program.testNames)
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
def test_NonExit(self):
|
2021-12-04 14:57:20 -04:00
|
|
|
stream = BufferedWriter()
|
2010-03-27 09:34:21 -03:00
|
|
|
program = unittest.main(exit=False,
|
|
|
|
argv=["foobar"],
|
2021-12-04 14:57:20 -04:00
|
|
|
testRunner=unittest.TextTestRunner(stream=stream),
|
2023-04-26 22:28:46 -03:00
|
|
|
testLoader=self.TestLoader(self.FooBar))
|
2010-03-27 09:34:21 -03:00
|
|
|
self.assertTrue(hasattr(program, 'result'))
|
2021-12-26 07:22:44 -04:00
|
|
|
out = stream.getvalue()
|
|
|
|
self.assertIn('\nFAIL: testFail ', out)
|
|
|
|
self.assertIn('\nERROR: testError ', out)
|
|
|
|
self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
|
|
|
|
expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
|
|
|
|
'expected failures=1, unexpected successes=1)\n')
|
|
|
|
self.assertTrue(out.endswith(expected))
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
def test_Exit(self):
|
2021-12-04 14:57:20 -04:00
|
|
|
stream = BufferedWriter()
|
2023-04-26 22:28:46 -03:00
|
|
|
with self.assertRaises(SystemExit) as cm:
|
|
|
|
unittest.main(
|
|
|
|
argv=["foobar"],
|
|
|
|
testRunner=unittest.TextTestRunner(stream=stream),
|
|
|
|
exit=True,
|
|
|
|
testLoader=self.TestLoader(self.FooBar))
|
|
|
|
self.assertEqual(cm.exception.code, 1)
|
2021-12-26 07:22:44 -04:00
|
|
|
out = stream.getvalue()
|
|
|
|
self.assertIn('\nFAIL: testFail ', out)
|
|
|
|
self.assertIn('\nERROR: testError ', out)
|
|
|
|
self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
|
|
|
|
expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
|
|
|
|
'expected failures=1, unexpected successes=1)\n')
|
|
|
|
self.assertTrue(out.endswith(expected))
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
def test_ExitAsDefault(self):
|
2021-12-04 14:57:20 -04:00
|
|
|
stream = BufferedWriter()
|
2023-04-26 22:28:46 -03:00
|
|
|
with self.assertRaises(SystemExit):
|
|
|
|
unittest.main(
|
|
|
|
argv=["foobar"],
|
|
|
|
testRunner=unittest.TextTestRunner(stream=stream),
|
|
|
|
testLoader=self.TestLoader(self.FooBar))
|
2021-12-26 07:22:44 -04:00
|
|
|
out = stream.getvalue()
|
|
|
|
self.assertIn('\nFAIL: testFail ', out)
|
|
|
|
self.assertIn('\nERROR: testError ', out)
|
|
|
|
self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
|
|
|
|
expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
|
|
|
|
'expected failures=1, unexpected successes=1)\n')
|
|
|
|
self.assertTrue(out.endswith(expected))
|
2010-05-07 13:00:30 -03:00
|
|
|
|
2024-01-09 15:50:01 -04:00
|
|
|
def test_ExitSkippedSuite(self):
|
|
|
|
stream = BufferedWriter()
|
|
|
|
with self.assertRaises(SystemExit) as cm:
|
|
|
|
unittest.main(
|
|
|
|
argv=["foobar", "-k", "testSkipped"],
|
|
|
|
testRunner=unittest.TextTestRunner(stream=stream),
|
|
|
|
testLoader=self.TestLoader(self.FooBar))
|
|
|
|
self.assertEqual(cm.exception.code, 0)
|
|
|
|
out = stream.getvalue()
|
|
|
|
expected = '\n\nOK (skipped=1)\n'
|
|
|
|
self.assertTrue(out.endswith(expected))
|
|
|
|
|
2023-04-26 22:28:46 -03:00
|
|
|
def test_ExitEmptySuite(self):
|
|
|
|
stream = BufferedWriter()
|
|
|
|
with self.assertRaises(SystemExit) as cm:
|
|
|
|
unittest.main(
|
|
|
|
argv=["empty"],
|
|
|
|
testRunner=unittest.TextTestRunner(stream=stream),
|
|
|
|
testLoader=self.TestLoader(self.Empty))
|
|
|
|
self.assertEqual(cm.exception.code, 5)
|
|
|
|
out = stream.getvalue()
|
|
|
|
self.assertIn('\nNO TESTS RAN\n', out)
|
|
|
|
|
2010-05-07 13:00:30 -03:00
|
|
|
|
|
|
|
class InitialisableProgram(unittest.TestProgram):
|
|
|
|
exit = False
|
|
|
|
result = None
|
|
|
|
verbosity = 1
|
|
|
|
defaultTest = None
|
2015-03-05 20:46:35 -04:00
|
|
|
tb_locals = False
|
2010-05-07 13:00:30 -03:00
|
|
|
testRunner = None
|
|
|
|
testLoader = unittest.defaultTestLoader
|
2010-12-18 23:59:10 -04:00
|
|
|
module = '__main__'
|
2010-05-07 13:00:30 -03:00
|
|
|
progName = 'test'
|
|
|
|
test = 'test'
|
|
|
|
def __init__(self, *args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
RESULT = object()
|
|
|
|
|
|
|
|
class FakeRunner(object):
|
|
|
|
initArgs = None
|
|
|
|
test = None
|
2015-03-05 20:46:35 -04:00
|
|
|
raiseError = 0
|
2010-05-07 13:00:30 -03:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
FakeRunner.initArgs = kwargs
|
|
|
|
if FakeRunner.raiseError:
|
2015-03-05 20:46:35 -04:00
|
|
|
FakeRunner.raiseError -= 1
|
2010-05-07 13:00:30 -03:00
|
|
|
raise TypeError
|
|
|
|
|
|
|
|
def run(self, test):
|
|
|
|
FakeRunner.test = test
|
|
|
|
return RESULT
|
|
|
|
|
2015-03-05 20:46:35 -04:00
|
|
|
|
2022-03-22 07:04:36 -03:00
|
|
|
@support.requires_subprocess()
|
2010-05-07 13:00:30 -03:00
|
|
|
class TestCommandLineArgs(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.program = InitialisableProgram()
|
|
|
|
self.program.createTests = lambda: None
|
|
|
|
FakeRunner.initArgs = None
|
|
|
|
FakeRunner.test = None
|
2015-03-05 20:46:35 -04:00
|
|
|
FakeRunner.raiseError = 0
|
2010-05-07 13:00:30 -03:00
|
|
|
|
|
|
|
def testVerbosity(self):
|
|
|
|
program = self.program
|
|
|
|
|
|
|
|
for opt in '-q', '--quiet':
|
|
|
|
program.verbosity = 1
|
|
|
|
program.parseArgs([None, opt])
|
|
|
|
self.assertEqual(program.verbosity, 0)
|
|
|
|
|
|
|
|
for opt in '-v', '--verbose':
|
|
|
|
program.verbosity = 1
|
|
|
|
program.parseArgs([None, opt])
|
|
|
|
self.assertEqual(program.verbosity, 2)
|
|
|
|
|
|
|
|
def testBufferCatchFailfast(self):
|
|
|
|
program = self.program
|
|
|
|
for arg, attr in (('buffer', 'buffer'), ('failfast', 'failfast'),
|
|
|
|
('catch', 'catchbreak')):
|
|
|
|
|
2013-08-29 06:37:28 -03:00
|
|
|
setattr(program, attr, None)
|
|
|
|
program.parseArgs([None])
|
|
|
|
self.assertIs(getattr(program, attr), False)
|
|
|
|
|
|
|
|
false = []
|
|
|
|
setattr(program, attr, false)
|
|
|
|
program.parseArgs([None])
|
|
|
|
self.assertIs(getattr(program, attr), false)
|
|
|
|
|
|
|
|
true = [42]
|
|
|
|
setattr(program, attr, true)
|
|
|
|
program.parseArgs([None])
|
|
|
|
self.assertIs(getattr(program, attr), true)
|
|
|
|
|
2010-05-07 13:00:30 -03:00
|
|
|
short_opt = '-%s' % arg[0]
|
|
|
|
long_opt = '--%s' % arg
|
|
|
|
for opt in short_opt, long_opt:
|
|
|
|
setattr(program, attr, None)
|
|
|
|
program.parseArgs([None, opt])
|
2013-08-29 06:37:28 -03:00
|
|
|
self.assertIs(getattr(program, attr), True)
|
|
|
|
|
|
|
|
setattr(program, attr, False)
|
|
|
|
with support.captured_stderr() as stderr, \
|
|
|
|
self.assertRaises(SystemExit) as cm:
|
|
|
|
program.parseArgs([None, opt])
|
|
|
|
self.assertEqual(cm.exception.args, (2,))
|
|
|
|
|
|
|
|
setattr(program, attr, True)
|
|
|
|
with support.captured_stderr() as stderr, \
|
|
|
|
self.assertRaises(SystemExit) as cm:
|
|
|
|
program.parseArgs([None, opt])
|
|
|
|
self.assertEqual(cm.exception.args, (2,))
|
2010-05-07 13:00:30 -03:00
|
|
|
|
2010-11-30 20:56:10 -04:00
|
|
|
def testWarning(self):
|
|
|
|
"""Test the warnings argument"""
|
|
|
|
# see #10535
|
|
|
|
class FakeTP(unittest.TestProgram):
|
|
|
|
def parseArgs(self, *args, **kw): pass
|
|
|
|
def runTests(self, *args, **kw): pass
|
2011-03-20 10:31:26 -03:00
|
|
|
warnoptions = sys.warnoptions[:]
|
2010-11-30 20:56:10 -04:00
|
|
|
try:
|
|
|
|
sys.warnoptions[:] = []
|
|
|
|
# no warn options, no arg -> default
|
|
|
|
self.assertEqual(FakeTP().warnings, 'default')
|
|
|
|
# no warn options, w/ arg -> arg value
|
|
|
|
self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore')
|
|
|
|
sys.warnoptions[:] = ['somevalue']
|
|
|
|
# warn options, no arg -> None
|
|
|
|
# warn options, w/ arg -> arg value
|
|
|
|
self.assertEqual(FakeTP().warnings, None)
|
|
|
|
self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore')
|
|
|
|
finally:
|
|
|
|
sys.warnoptions[:] = warnoptions
|
|
|
|
|
2010-05-07 13:00:30 -03:00
|
|
|
def testRunTestsRunnerClass(self):
|
|
|
|
program = self.program
|
|
|
|
|
|
|
|
program.testRunner = FakeRunner
|
|
|
|
program.verbosity = 'verbosity'
|
|
|
|
program.failfast = 'failfast'
|
|
|
|
program.buffer = 'buffer'
|
2010-11-30 20:56:10 -04:00
|
|
|
program.warnings = 'warnings'
|
2023-04-02 19:12:51 -03:00
|
|
|
program.durations = '5'
|
2010-05-07 13:00:30 -03:00
|
|
|
|
|
|
|
program.runTests()
|
|
|
|
|
|
|
|
self.assertEqual(FakeRunner.initArgs, {'verbosity': 'verbosity',
|
|
|
|
'failfast': 'failfast',
|
2010-11-30 20:56:10 -04:00
|
|
|
'buffer': 'buffer',
|
2015-03-05 20:46:35 -04:00
|
|
|
'tb_locals': False,
|
2023-04-02 19:12:51 -03:00
|
|
|
'warnings': 'warnings',
|
|
|
|
'durations': '5'})
|
2010-05-07 13:00:30 -03:00
|
|
|
self.assertEqual(FakeRunner.test, 'test')
|
|
|
|
self.assertIs(program.result, RESULT)
|
|
|
|
|
|
|
|
def testRunTestsRunnerInstance(self):
|
|
|
|
program = self.program
|
|
|
|
|
|
|
|
program.testRunner = FakeRunner()
|
|
|
|
FakeRunner.initArgs = None
|
|
|
|
|
|
|
|
program.runTests()
|
|
|
|
|
|
|
|
# A new FakeRunner should not have been instantiated
|
|
|
|
self.assertIsNone(FakeRunner.initArgs)
|
|
|
|
|
|
|
|
self.assertEqual(FakeRunner.test, 'test')
|
|
|
|
self.assertIs(program.result, RESULT)
|
|
|
|
|
2015-03-05 20:46:35 -04:00
|
|
|
def test_locals(self):
|
|
|
|
program = self.program
|
|
|
|
|
|
|
|
program.testRunner = FakeRunner
|
|
|
|
program.parseArgs([None, '--locals'])
|
|
|
|
self.assertEqual(True, program.tb_locals)
|
|
|
|
program.runTests()
|
|
|
|
self.assertEqual(FakeRunner.initArgs, {'buffer': False,
|
|
|
|
'failfast': False,
|
|
|
|
'tb_locals': True,
|
|
|
|
'verbosity': 1,
|
2023-04-02 19:12:51 -03:00
|
|
|
'warnings': None,
|
|
|
|
'durations': None})
|
2015-03-05 20:46:35 -04:00
|
|
|
|
2010-05-07 13:00:30 -03:00
|
|
|
def testRunTestsOldRunnerClass(self):
|
|
|
|
program = self.program
|
|
|
|
|
2015-03-05 20:46:35 -04:00
|
|
|
# Two TypeErrors are needed to fall all the way back to old-style
|
|
|
|
# runners - one to fail tb_locals, one to fail buffer etc.
|
|
|
|
FakeRunner.raiseError = 2
|
2010-05-07 13:00:30 -03:00
|
|
|
program.testRunner = FakeRunner
|
|
|
|
program.verbosity = 'verbosity'
|
|
|
|
program.failfast = 'failfast'
|
|
|
|
program.buffer = 'buffer'
|
|
|
|
program.test = 'test'
|
2023-04-02 19:12:51 -03:00
|
|
|
program.durations = '0'
|
2010-05-07 13:00:30 -03:00
|
|
|
|
|
|
|
program.runTests()
|
|
|
|
|
|
|
|
# If initialising raises a type error it should be retried
|
|
|
|
# without the new keyword arguments
|
|
|
|
self.assertEqual(FakeRunner.initArgs, {})
|
|
|
|
self.assertEqual(FakeRunner.test, 'test')
|
|
|
|
self.assertIs(program.result, RESULT)
|
|
|
|
|
|
|
|
def testCatchBreakInstallsHandler(self):
|
|
|
|
module = sys.modules['unittest.main']
|
|
|
|
original = module.installHandler
|
|
|
|
def restore():
|
|
|
|
module.installHandler = original
|
|
|
|
self.addCleanup(restore)
|
|
|
|
|
|
|
|
self.installed = False
|
|
|
|
def fakeInstallHandler():
|
|
|
|
self.installed = True
|
|
|
|
module.installHandler = fakeInstallHandler
|
|
|
|
|
|
|
|
program = self.program
|
|
|
|
program.catchbreak = True
|
2023-04-02 19:12:51 -03:00
|
|
|
program.durations = None
|
2010-05-07 13:00:30 -03:00
|
|
|
|
|
|
|
program.testRunner = FakeRunner
|
|
|
|
|
|
|
|
program.runTests()
|
|
|
|
self.assertTrue(self.installed)
|
|
|
|
|
2010-12-03 21:11:21 -04:00
|
|
|
def _patch_isfile(self, names, exists=True):
|
|
|
|
def isfile(path):
|
|
|
|
return path in names
|
|
|
|
original = os.path.isfile
|
|
|
|
os.path.isfile = isfile
|
|
|
|
def restore():
|
|
|
|
os.path.isfile = original
|
|
|
|
self.addCleanup(restore)
|
|
|
|
|
|
|
|
|
|
|
|
def testParseArgsFileNames(self):
|
|
|
|
# running tests with filenames instead of module names
|
|
|
|
program = self.program
|
|
|
|
argv = ['progname', 'foo.py', 'bar.Py', 'baz.PY', 'wing.txt']
|
|
|
|
self._patch_isfile(argv)
|
|
|
|
|
|
|
|
program.createTests = lambda: None
|
|
|
|
program.parseArgs(argv)
|
|
|
|
|
|
|
|
# note that 'wing.txt' is not a Python file so the name should
|
|
|
|
# *not* be converted to a module name
|
|
|
|
expected = ['foo', 'bar', 'baz', 'wing.txt']
|
|
|
|
self.assertEqual(program.testNames, expected)
|
|
|
|
|
|
|
|
|
|
|
|
def testParseArgsFilePaths(self):
|
|
|
|
program = self.program
|
|
|
|
argv = ['progname', 'foo/bar/baz.py', 'green\\red.py']
|
|
|
|
self._patch_isfile(argv)
|
|
|
|
|
|
|
|
program.createTests = lambda: None
|
|
|
|
program.parseArgs(argv)
|
|
|
|
|
|
|
|
expected = ['foo.bar.baz', 'green.red']
|
|
|
|
self.assertEqual(program.testNames, expected)
|
|
|
|
|
|
|
|
|
|
|
|
def testParseArgsNonExistentFiles(self):
|
|
|
|
program = self.program
|
|
|
|
argv = ['progname', 'foo/bar/baz.py', 'green\\red.py']
|
|
|
|
self._patch_isfile([])
|
|
|
|
|
|
|
|
program.createTests = lambda: None
|
|
|
|
program.parseArgs(argv)
|
|
|
|
|
|
|
|
self.assertEqual(program.testNames, argv[1:])
|
|
|
|
|
|
|
|
def testParseArgsAbsolutePathsThatCanBeConverted(self):
|
|
|
|
cur_dir = os.getcwd()
|
|
|
|
program = self.program
|
|
|
|
def _join(name):
|
|
|
|
return os.path.join(cur_dir, name)
|
|
|
|
argv = ['progname', _join('foo/bar/baz.py'), _join('green\\red.py')]
|
|
|
|
self._patch_isfile(argv)
|
|
|
|
|
|
|
|
program.createTests = lambda: None
|
|
|
|
program.parseArgs(argv)
|
|
|
|
|
|
|
|
expected = ['foo.bar.baz', 'green.red']
|
|
|
|
self.assertEqual(program.testNames, expected)
|
|
|
|
|
|
|
|
def testParseArgsAbsolutePathsThatCannotBeConverted(self):
|
|
|
|
program = self.program
|
2024-01-13 03:36:05 -04:00
|
|
|
drive = os.path.splitdrive(os.getcwd())[0]
|
|
|
|
argv = ['progname', f'{drive}/foo/bar/baz.py', f'{drive}/green/red.py']
|
2010-12-03 21:11:21 -04:00
|
|
|
self._patch_isfile(argv)
|
|
|
|
|
|
|
|
program.createTests = lambda: None
|
|
|
|
program.parseArgs(argv)
|
|
|
|
|
|
|
|
self.assertEqual(program.testNames, argv[1:])
|
|
|
|
|
|
|
|
# it may be better to use platform specific functions to normalise paths
|
2013-08-17 10:11:40 -03:00
|
|
|
# rather than accepting '.PY' and '\' as file separator on Linux / Mac
|
2010-12-03 21:11:21 -04:00
|
|
|
# it would also be better to check that a filename is a valid module
|
|
|
|
# identifier (we have a regex for this in loader.py)
|
|
|
|
# for invalid filenames should we raise a useful error rather than
|
|
|
|
# leaving the current error message (import of filename fails) in place?
|
|
|
|
|
2017-11-25 11:23:52 -04:00
|
|
|
def testParseArgsSelectedTestNames(self):
|
|
|
|
program = self.program
|
|
|
|
argv = ['progname', '-k', 'foo', '-k', 'bar', '-k', '*pat*']
|
|
|
|
|
|
|
|
program.createTests = lambda: None
|
|
|
|
program.parseArgs(argv)
|
|
|
|
|
|
|
|
self.assertEqual(program.testNamePatterns, ['*foo*', '*bar*', '*pat*'])
|
|
|
|
|
|
|
|
def testSelectedTestNamesFunctionalTest(self):
|
|
|
|
def run_unittest(args):
|
2022-05-05 22:41:24 -03:00
|
|
|
# Use -E to ignore PYTHONSAFEPATH env var
|
|
|
|
cmd = [sys.executable, '-E', '-m', 'unittest'] + args
|
|
|
|
p = subprocess.Popen(cmd,
|
2017-11-25 11:23:52 -04:00
|
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, cwd=os.path.dirname(__file__))
|
|
|
|
with p:
|
|
|
|
_, stderr = p.communicate()
|
|
|
|
return stderr.decode()
|
|
|
|
|
|
|
|
t = '_test_warnings'
|
2022-06-26 04:18:06 -03:00
|
|
|
self.assertIn('Ran 5 tests', run_unittest([t]))
|
|
|
|
self.assertIn('Ran 5 tests', run_unittest(['-k', 'TestWarnings', t]))
|
|
|
|
self.assertIn('Ran 5 tests', run_unittest(['discover', '-p', '*_test*', '-k', 'TestWarnings']))
|
|
|
|
self.assertIn('Ran 1 test ', run_unittest(['-k', 'f', t]))
|
|
|
|
self.assertIn('Ran 5 tests', run_unittest(['-k', 't', t]))
|
|
|
|
self.assertIn('Ran 2 tests', run_unittest(['-k', '*t', t]))
|
|
|
|
self.assertIn('Ran 5 tests', run_unittest(['-k', '*test_warnings.*Warning*', t]))
|
|
|
|
self.assertIn('Ran 1 test ', run_unittest(['-k', '*test_warnings.*warning*', t]))
|
2017-11-25 11:23:52 -04:00
|
|
|
|
2010-05-07 13:00:30 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|