2014-09-29 22:54:28 -03:00
|
|
|
import os.path
|
|
|
|
from os.path import abspath
|
2010-05-07 20:42:40 -03:00
|
|
|
import re
|
2010-03-27 09:34:21 -03:00
|
|
|
import sys
|
2013-11-23 09:29:23 -04:00
|
|
|
import types
|
|
|
|
import builtins
|
2013-08-29 06:37:28 -03:00
|
|
|
from test import support
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
import unittest
|
2014-08-06 21:20:22 -03:00
|
|
|
import unittest.test
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
|
2010-11-21 17:28:01 -04:00
|
|
|
class TestableTestProgram(unittest.TestProgram):
|
2013-08-29 06:37:28 -03:00
|
|
|
module = None
|
2010-11-21 17:28:01 -04:00
|
|
|
exit = True
|
|
|
|
defaultTest = failfast = catchbreak = buffer = None
|
|
|
|
verbosity = 1
|
|
|
|
progName = ''
|
|
|
|
testRunner = testLoader = None
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
class TestDiscovery(unittest.TestCase):
|
|
|
|
|
|
|
|
# Heavily mocked tests so I can avoid hitting the filesystem
|
|
|
|
def test_get_name_from_path(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
loader._top_level_dir = '/foo'
|
|
|
|
name = loader._get_name_from_path('/foo/bar/baz.py')
|
|
|
|
self.assertEqual(name, 'bar.baz')
|
|
|
|
|
|
|
|
if not __debug__:
|
|
|
|
# asserts are off
|
|
|
|
return
|
|
|
|
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
loader._get_name_from_path('/bar/baz.py')
|
|
|
|
|
|
|
|
def test_find_tests(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
original_listdir = os.listdir
|
|
|
|
def restore_listdir():
|
|
|
|
os.listdir = original_listdir
|
|
|
|
original_isfile = os.path.isfile
|
|
|
|
def restore_isfile():
|
|
|
|
os.path.isfile = original_isfile
|
|
|
|
original_isdir = os.path.isdir
|
|
|
|
def restore_isdir():
|
|
|
|
os.path.isdir = original_isdir
|
|
|
|
|
2013-03-18 21:50:12 -03:00
|
|
|
path_lists = [['test2.py', 'test1.py', 'not_a_test.py', 'test_dir',
|
2010-03-27 09:34:21 -03:00
|
|
|
'test.foo', 'test-not-a-module.py', 'another_dir'],
|
2013-03-18 21:50:12 -03:00
|
|
|
['test4.py', 'test3.py', ]]
|
2010-03-27 09:34:21 -03:00
|
|
|
os.listdir = lambda path: path_lists.pop(0)
|
|
|
|
self.addCleanup(restore_listdir)
|
|
|
|
|
|
|
|
def isdir(path):
|
|
|
|
return path.endswith('dir')
|
|
|
|
os.path.isdir = isdir
|
|
|
|
self.addCleanup(restore_isdir)
|
|
|
|
|
|
|
|
def isfile(path):
|
|
|
|
# another_dir is not a package and so shouldn't be recursed into
|
|
|
|
return not path.endswith('dir') and not 'another_dir' in path
|
|
|
|
os.path.isfile = isfile
|
|
|
|
self.addCleanup(restore_isfile)
|
|
|
|
|
|
|
|
loader._get_module_from_name = lambda path: path + ' module'
|
2014-09-08 15:21:37 -03:00
|
|
|
orig_load_tests = loader.loadTestsFromModule
|
|
|
|
def loadTestsFromModule(module, pattern=None):
|
|
|
|
# This is where load_tests is called.
|
|
|
|
base = orig_load_tests(module, pattern=pattern)
|
|
|
|
return base + [module + ' tests']
|
|
|
|
loader.loadTestsFromModule = loadTestsFromModule
|
|
|
|
loader.suiteClass = lambda thing: thing
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2010-05-07 20:42:40 -03:00
|
|
|
top_level = os.path.abspath('/foo')
|
|
|
|
loader._top_level_dir = top_level
|
|
|
|
suite = list(loader._find_tests(top_level, 'test*.py'))
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2013-03-18 21:50:12 -03:00
|
|
|
# The test suites found should be sorted alphabetically for reliable
|
|
|
|
# execution order.
|
2014-09-08 15:21:37 -03:00
|
|
|
expected = [[name + ' module tests'] for name in
|
|
|
|
('test1', 'test2', 'test_dir')]
|
|
|
|
expected.extend([[('test_dir.%s' % name) + ' module tests'] for name in
|
2010-03-27 09:34:21 -03:00
|
|
|
('test3', 'test4')])
|
|
|
|
self.assertEqual(suite, expected)
|
|
|
|
|
|
|
|
def test_find_tests_with_package(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
original_listdir = os.listdir
|
|
|
|
def restore_listdir():
|
|
|
|
os.listdir = original_listdir
|
|
|
|
original_isfile = os.path.isfile
|
|
|
|
def restore_isfile():
|
|
|
|
os.path.isfile = original_isfile
|
|
|
|
original_isdir = os.path.isdir
|
|
|
|
def restore_isdir():
|
|
|
|
os.path.isdir = original_isdir
|
|
|
|
|
|
|
|
directories = ['a_directory', 'test_directory', 'test_directory2']
|
|
|
|
path_lists = [directories, [], [], []]
|
|
|
|
os.listdir = lambda path: path_lists.pop(0)
|
|
|
|
self.addCleanup(restore_listdir)
|
|
|
|
|
|
|
|
os.path.isdir = lambda path: True
|
|
|
|
self.addCleanup(restore_isdir)
|
|
|
|
|
|
|
|
os.path.isfile = lambda path: os.path.basename(path) not in directories
|
|
|
|
self.addCleanup(restore_isfile)
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
paths = []
|
|
|
|
load_tests_args = []
|
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
self.paths.append(path)
|
|
|
|
if os.path.basename(path) == 'test_directory':
|
|
|
|
def load_tests(loader, tests, pattern):
|
|
|
|
self.load_tests_args.append((loader, tests, pattern))
|
2014-09-08 15:21:37 -03:00
|
|
|
return [self.path + ' load_tests']
|
2010-03-27 09:34:21 -03:00
|
|
|
self.load_tests = load_tests
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.path == other.path
|
|
|
|
|
|
|
|
loader._get_module_from_name = lambda name: Module(name)
|
2014-09-08 15:21:37 -03:00
|
|
|
orig_load_tests = loader.loadTestsFromModule
|
|
|
|
def loadTestsFromModule(module, pattern=None):
|
|
|
|
# This is where load_tests is called.
|
|
|
|
base = orig_load_tests(module, pattern=pattern)
|
|
|
|
return base + [module.path + ' module tests']
|
2010-03-27 09:34:21 -03:00
|
|
|
loader.loadTestsFromModule = loadTestsFromModule
|
2014-09-08 15:21:37 -03:00
|
|
|
loader.suiteClass = lambda thing: thing
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
loader._top_level_dir = '/foo'
|
|
|
|
# this time no '.py' on the pattern so that it can match
|
|
|
|
# a test package
|
|
|
|
suite = list(loader._find_tests('/foo', 'test*'))
|
|
|
|
|
2014-09-08 15:21:37 -03:00
|
|
|
# We should have loaded tests from the a_directory and test_directory2
|
|
|
|
# directly and via load_tests for the test_directory package, which
|
|
|
|
# still calls the baseline module loader.
|
2010-03-27 09:34:21 -03:00
|
|
|
self.assertEqual(suite,
|
2014-09-08 15:21:37 -03:00
|
|
|
[['a_directory module tests'],
|
|
|
|
['test_directory load_tests',
|
|
|
|
'test_directory module tests'],
|
|
|
|
['test_directory2 module tests']])
|
|
|
|
|
|
|
|
|
2013-03-18 21:50:12 -03:00
|
|
|
# The test module paths should be sorted for reliable execution order
|
2014-09-08 15:21:37 -03:00
|
|
|
self.assertEqual(Module.paths,
|
|
|
|
['a_directory', 'test_directory', 'test_directory2'])
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
# load_tests should have been called once with loader, tests and pattern
|
2014-09-08 15:21:37 -03:00
|
|
|
# (but there are no tests in our stub module itself, so thats [] at the
|
|
|
|
# time of call.
|
2010-03-27 09:34:21 -03:00
|
|
|
self.assertEqual(Module.load_tests_args,
|
2014-09-08 15:21:37 -03:00
|
|
|
[(loader, [], 'test*')])
|
|
|
|
|
|
|
|
def test_find_tests_default_calls_package_load_tests(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
original_listdir = os.listdir
|
|
|
|
def restore_listdir():
|
|
|
|
os.listdir = original_listdir
|
|
|
|
original_isfile = os.path.isfile
|
|
|
|
def restore_isfile():
|
|
|
|
os.path.isfile = original_isfile
|
|
|
|
original_isdir = os.path.isdir
|
|
|
|
def restore_isdir():
|
|
|
|
os.path.isdir = original_isdir
|
|
|
|
|
|
|
|
directories = ['a_directory', 'test_directory', 'test_directory2']
|
|
|
|
path_lists = [directories, [], [], []]
|
|
|
|
os.listdir = lambda path: path_lists.pop(0)
|
|
|
|
self.addCleanup(restore_listdir)
|
|
|
|
|
|
|
|
os.path.isdir = lambda path: True
|
|
|
|
self.addCleanup(restore_isdir)
|
|
|
|
|
|
|
|
os.path.isfile = lambda path: os.path.basename(path) not in directories
|
|
|
|
self.addCleanup(restore_isfile)
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
paths = []
|
|
|
|
load_tests_args = []
|
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
self.paths.append(path)
|
|
|
|
if os.path.basename(path) == 'test_directory':
|
|
|
|
def load_tests(loader, tests, pattern):
|
|
|
|
self.load_tests_args.append((loader, tests, pattern))
|
|
|
|
return [self.path + ' load_tests']
|
|
|
|
self.load_tests = load_tests
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.path == other.path
|
|
|
|
|
|
|
|
loader._get_module_from_name = lambda name: Module(name)
|
|
|
|
orig_load_tests = loader.loadTestsFromModule
|
|
|
|
def loadTestsFromModule(module, pattern=None):
|
|
|
|
# This is where load_tests is called.
|
|
|
|
base = orig_load_tests(module, pattern=pattern)
|
|
|
|
return base + [module.path + ' module tests']
|
|
|
|
loader.loadTestsFromModule = loadTestsFromModule
|
|
|
|
loader.suiteClass = lambda thing: thing
|
|
|
|
|
|
|
|
loader._top_level_dir = '/foo'
|
|
|
|
# this time no '.py' on the pattern so that it can match
|
|
|
|
# a test package
|
|
|
|
suite = list(loader._find_tests('/foo', 'test*.py'))
|
|
|
|
|
|
|
|
# We should have loaded tests from the a_directory and test_directory2
|
|
|
|
# directly and via load_tests for the test_directory package, which
|
|
|
|
# still calls the baseline module loader.
|
|
|
|
self.assertEqual(suite,
|
|
|
|
[['a_directory module tests'],
|
|
|
|
['test_directory load_tests',
|
|
|
|
'test_directory module tests'],
|
|
|
|
['test_directory2 module tests']])
|
|
|
|
# The test module paths should be sorted for reliable execution order
|
|
|
|
self.assertEqual(Module.paths,
|
|
|
|
['a_directory', 'test_directory', 'test_directory2'])
|
|
|
|
|
|
|
|
|
|
|
|
# load_tests should have been called once with loader, tests and pattern
|
|
|
|
self.assertEqual(Module.load_tests_args,
|
|
|
|
[(loader, [], 'test*.py')])
|
|
|
|
|
|
|
|
def test_find_tests_customise_via_package_pattern(self):
|
|
|
|
# This test uses the example 'do-nothing' load_tests from
|
|
|
|
# https://docs.python.org/3/library/unittest.html#load-tests-protocol
|
|
|
|
# to make sure that that actually works.
|
|
|
|
# Housekeeping
|
|
|
|
original_listdir = os.listdir
|
|
|
|
def restore_listdir():
|
|
|
|
os.listdir = original_listdir
|
|
|
|
self.addCleanup(restore_listdir)
|
|
|
|
original_isfile = os.path.isfile
|
|
|
|
def restore_isfile():
|
|
|
|
os.path.isfile = original_isfile
|
|
|
|
self.addCleanup(restore_isfile)
|
|
|
|
original_isdir = os.path.isdir
|
|
|
|
def restore_isdir():
|
|
|
|
os.path.isdir = original_isdir
|
|
|
|
self.addCleanup(restore_isdir)
|
2014-09-29 22:54:28 -03:00
|
|
|
self.addCleanup(sys.path.remove, abspath('/foo'))
|
2014-09-08 15:21:37 -03:00
|
|
|
|
|
|
|
# Test data: we expect the following:
|
|
|
|
# a listdir to find our package, and a isfile and isdir check on it.
|
|
|
|
# a module-from-name call to turn that into a module
|
|
|
|
# followed by load_tests.
|
|
|
|
# then our load_tests will call discover() which is messy
|
|
|
|
# but that finally chains into find_tests again for the child dir -
|
|
|
|
# which is why we don't have a infinite loop.
|
|
|
|
# We expect to see:
|
|
|
|
# the module load tests for both package and plain module called,
|
|
|
|
# and the plain module result nested by the package module load_tests
|
|
|
|
# indicating that it was processed and could have been mutated.
|
2014-09-29 22:54:28 -03:00
|
|
|
vfs = {abspath('/foo'): ['my_package'],
|
|
|
|
abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
|
2014-09-08 15:21:37 -03:00
|
|
|
def list_dir(path):
|
|
|
|
return list(vfs[path])
|
|
|
|
os.listdir = list_dir
|
|
|
|
os.path.isdir = lambda path: not path.endswith('.py')
|
|
|
|
os.path.isfile = lambda path: path.endswith('.py')
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
paths = []
|
|
|
|
load_tests_args = []
|
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
self.paths.append(path)
|
|
|
|
if path.endswith('test_module'):
|
|
|
|
def load_tests(loader, tests, pattern):
|
|
|
|
self.load_tests_args.append((loader, tests, pattern))
|
|
|
|
return [self.path + ' load_tests']
|
|
|
|
else:
|
|
|
|
def load_tests(loader, tests, pattern):
|
|
|
|
self.load_tests_args.append((loader, tests, pattern))
|
|
|
|
# top level directory cached on loader instance
|
|
|
|
__file__ = '/foo/my_package/__init__.py'
|
|
|
|
this_dir = os.path.dirname(__file__)
|
|
|
|
pkg_tests = loader.discover(
|
|
|
|
start_dir=this_dir, pattern=pattern)
|
|
|
|
return [self.path + ' load_tests', tests
|
|
|
|
] + pkg_tests
|
|
|
|
self.load_tests = load_tests
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.path == other.path
|
|
|
|
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
loader._get_module_from_name = lambda name: Module(name)
|
|
|
|
loader.suiteClass = lambda thing: thing
|
|
|
|
|
2014-09-29 22:54:28 -03:00
|
|
|
loader._top_level_dir = abspath('/foo')
|
2014-09-08 15:21:37 -03:00
|
|
|
# this time no '.py' on the pattern so that it can match
|
|
|
|
# a test package
|
2014-09-29 22:54:28 -03:00
|
|
|
suite = list(loader._find_tests(abspath('/foo'), 'test*.py'))
|
2014-09-08 15:21:37 -03:00
|
|
|
|
|
|
|
# We should have loaded tests from both my_package and
|
|
|
|
# my_pacakge.test_module, and also run the load_tests hook in both.
|
|
|
|
# (normally this would be nested TestSuites.)
|
|
|
|
self.assertEqual(suite,
|
|
|
|
[['my_package load_tests', [],
|
|
|
|
['my_package.test_module load_tests']]])
|
|
|
|
# Parents before children.
|
|
|
|
self.assertEqual(Module.paths,
|
|
|
|
['my_package', 'my_package.test_module'])
|
|
|
|
|
|
|
|
# load_tests should have been called twice with loader, tests and pattern
|
|
|
|
self.assertEqual(Module.load_tests_args,
|
|
|
|
[(loader, [], 'test*.py'),
|
|
|
|
(loader, [], 'test*.py')])
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
def test_discover(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
original_isfile = os.path.isfile
|
Merged revisions 79464,79471,79623,79626,79630,79632,79643,79648-79649,79679,79685,79711,79761,79774,79777,79792-79794,79877,79898-79900 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r79464 | michael.foord | 2010-03-27 07:55:19 -0500 (Sat, 27 Mar 2010) | 1 line
A fix for running unittest tests on platforms without the audioop module (e.g. jython and IronPython)
........
r79471 | michael.foord | 2010-03-27 14:10:11 -0500 (Sat, 27 Mar 2010) | 4 lines
Addition of delta keyword argument to unittest.TestCase.assertAlmostEquals and assertNotAlmostEquals
This allows the comparison of objects by specifying a maximum difference; this includes the comparing of non-numeric objects that don't support rounding.
........
r79623 | michael.foord | 2010-04-02 16:42:47 -0500 (Fri, 02 Apr 2010) | 1 line
Addition of -b command line option to unittest for buffering stdout and stderr during test runs.
........
r79626 | michael.foord | 2010-04-02 17:08:29 -0500 (Fri, 02 Apr 2010) | 1 line
TestResult stores original sys.stdout and tests no longer use sys.__stdout__ (etc) in tests for unittest -b command line option
........
r79630 | michael.foord | 2010-04-02 17:30:56 -0500 (Fri, 02 Apr 2010) | 1 line
unittest tests no longer replace the sys.stdout put in place by regrtest
........
r79632 | michael.foord | 2010-04-02 17:55:59 -0500 (Fri, 02 Apr 2010) | 1 line
Issue #8038: Addition of unittest.TestCase.assertNotRegexpMatches
........
r79643 | michael.foord | 2010-04-02 20:15:21 -0500 (Fri, 02 Apr 2010) | 1 line
Support dotted module names for test discovery paths in unittest. Issue 8038.
........
r79648 | michael.foord | 2010-04-02 21:21:39 -0500 (Fri, 02 Apr 2010) | 1 line
Cross platform unittest.TestResult newline handling when buffering stdout / stderr.
........
r79649 | michael.foord | 2010-04-02 21:33:55 -0500 (Fri, 02 Apr 2010) | 1 line
Another attempt at a fix for unittest.test.test_result for windows line endings
........
r79679 | michael.foord | 2010-04-03 09:52:18 -0500 (Sat, 03 Apr 2010) | 1 line
Adding -b command line option to the unittest usage message.
........
r79685 | michael.foord | 2010-04-03 10:20:00 -0500 (Sat, 03 Apr 2010) | 1 line
Minor tweak to unittest command line usage message
........
r79711 | michael.foord | 2010-04-03 12:03:11 -0500 (Sat, 03 Apr 2010) | 1 line
Documenting new features in unittest
........
r79761 | michael.foord | 2010-04-04 17:41:54 -0500 (Sun, 04 Apr 2010) | 1 line
unittest documentation formatting changes
........
r79774 | michael.foord | 2010-04-04 18:28:44 -0500 (Sun, 04 Apr 2010) | 1 line
Adding documentation for new unittest.main() parameters
........
r79777 | michael.foord | 2010-04-04 19:39:50 -0500 (Sun, 04 Apr 2010) | 1 line
Document signal handling functions in unittest.rst
........
r79792 | michael.foord | 2010-04-05 05:26:26 -0500 (Mon, 05 Apr 2010) | 1 line
Documentation fixes for unittest
........
r79793 | michael.foord | 2010-04-05 05:28:27 -0500 (Mon, 05 Apr 2010) | 1 line
Furterh documentation fix for unittest.rst
........
r79794 | michael.foord | 2010-04-05 05:30:14 -0500 (Mon, 05 Apr 2010) | 1 line
Further documentation fix for unittest.rst
........
r79877 | michael.foord | 2010-04-06 18:18:16 -0500 (Tue, 06 Apr 2010) | 1 line
Fix module directory finding logic for dotted paths in unittest test discovery.
........
r79898 | michael.foord | 2010-04-07 18:04:22 -0500 (Wed, 07 Apr 2010) | 1 line
unittest.result.TestResult does not create its buffers until they're used. It uses StringIO not cStringIO. Issue 8333.
........
r79899 | michael.foord | 2010-04-07 19:04:24 -0500 (Wed, 07 Apr 2010) | 1 line
Switch regrtest to use StringIO instead of cStringIO for test_multiprocessing on Windows. Issue 8333.
........
r79900 | michael.foord | 2010-04-07 23:33:20 -0500 (Wed, 07 Apr 2010) | 1 line
Correction of unittest documentation typos and omissions
........
2010-04-11 17:43:16 -03:00
|
|
|
original_isdir = os.path.isdir
|
2010-03-27 09:34:21 -03:00
|
|
|
def restore_isfile():
|
|
|
|
os.path.isfile = original_isfile
|
|
|
|
|
|
|
|
os.path.isfile = lambda path: False
|
|
|
|
self.addCleanup(restore_isfile)
|
|
|
|
|
|
|
|
orig_sys_path = sys.path[:]
|
|
|
|
def restore_path():
|
|
|
|
sys.path[:] = orig_sys_path
|
|
|
|
self.addCleanup(restore_path)
|
|
|
|
|
|
|
|
full_path = os.path.abspath(os.path.normpath('/foo'))
|
|
|
|
with self.assertRaises(ImportError):
|
|
|
|
loader.discover('/foo/bar', top_level_dir='/foo')
|
|
|
|
|
|
|
|
self.assertEqual(loader._top_level_dir, full_path)
|
|
|
|
self.assertIn(full_path, sys.path)
|
|
|
|
|
|
|
|
os.path.isfile = lambda path: True
|
Merged revisions 79464,79471,79623,79626,79630,79632,79643,79648-79649,79679,79685,79711,79761,79774,79777,79792-79794,79877,79898-79900 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r79464 | michael.foord | 2010-03-27 07:55:19 -0500 (Sat, 27 Mar 2010) | 1 line
A fix for running unittest tests on platforms without the audioop module (e.g. jython and IronPython)
........
r79471 | michael.foord | 2010-03-27 14:10:11 -0500 (Sat, 27 Mar 2010) | 4 lines
Addition of delta keyword argument to unittest.TestCase.assertAlmostEquals and assertNotAlmostEquals
This allows the comparison of objects by specifying a maximum difference; this includes the comparing of non-numeric objects that don't support rounding.
........
r79623 | michael.foord | 2010-04-02 16:42:47 -0500 (Fri, 02 Apr 2010) | 1 line
Addition of -b command line option to unittest for buffering stdout and stderr during test runs.
........
r79626 | michael.foord | 2010-04-02 17:08:29 -0500 (Fri, 02 Apr 2010) | 1 line
TestResult stores original sys.stdout and tests no longer use sys.__stdout__ (etc) in tests for unittest -b command line option
........
r79630 | michael.foord | 2010-04-02 17:30:56 -0500 (Fri, 02 Apr 2010) | 1 line
unittest tests no longer replace the sys.stdout put in place by regrtest
........
r79632 | michael.foord | 2010-04-02 17:55:59 -0500 (Fri, 02 Apr 2010) | 1 line
Issue #8038: Addition of unittest.TestCase.assertNotRegexpMatches
........
r79643 | michael.foord | 2010-04-02 20:15:21 -0500 (Fri, 02 Apr 2010) | 1 line
Support dotted module names for test discovery paths in unittest. Issue 8038.
........
r79648 | michael.foord | 2010-04-02 21:21:39 -0500 (Fri, 02 Apr 2010) | 1 line
Cross platform unittest.TestResult newline handling when buffering stdout / stderr.
........
r79649 | michael.foord | 2010-04-02 21:33:55 -0500 (Fri, 02 Apr 2010) | 1 line
Another attempt at a fix for unittest.test.test_result for windows line endings
........
r79679 | michael.foord | 2010-04-03 09:52:18 -0500 (Sat, 03 Apr 2010) | 1 line
Adding -b command line option to the unittest usage message.
........
r79685 | michael.foord | 2010-04-03 10:20:00 -0500 (Sat, 03 Apr 2010) | 1 line
Minor tweak to unittest command line usage message
........
r79711 | michael.foord | 2010-04-03 12:03:11 -0500 (Sat, 03 Apr 2010) | 1 line
Documenting new features in unittest
........
r79761 | michael.foord | 2010-04-04 17:41:54 -0500 (Sun, 04 Apr 2010) | 1 line
unittest documentation formatting changes
........
r79774 | michael.foord | 2010-04-04 18:28:44 -0500 (Sun, 04 Apr 2010) | 1 line
Adding documentation for new unittest.main() parameters
........
r79777 | michael.foord | 2010-04-04 19:39:50 -0500 (Sun, 04 Apr 2010) | 1 line
Document signal handling functions in unittest.rst
........
r79792 | michael.foord | 2010-04-05 05:26:26 -0500 (Mon, 05 Apr 2010) | 1 line
Documentation fixes for unittest
........
r79793 | michael.foord | 2010-04-05 05:28:27 -0500 (Mon, 05 Apr 2010) | 1 line
Furterh documentation fix for unittest.rst
........
r79794 | michael.foord | 2010-04-05 05:30:14 -0500 (Mon, 05 Apr 2010) | 1 line
Further documentation fix for unittest.rst
........
r79877 | michael.foord | 2010-04-06 18:18:16 -0500 (Tue, 06 Apr 2010) | 1 line
Fix module directory finding logic for dotted paths in unittest test discovery.
........
r79898 | michael.foord | 2010-04-07 18:04:22 -0500 (Wed, 07 Apr 2010) | 1 line
unittest.result.TestResult does not create its buffers until they're used. It uses StringIO not cStringIO. Issue 8333.
........
r79899 | michael.foord | 2010-04-07 19:04:24 -0500 (Wed, 07 Apr 2010) | 1 line
Switch regrtest to use StringIO instead of cStringIO for test_multiprocessing on Windows. Issue 8333.
........
r79900 | michael.foord | 2010-04-07 23:33:20 -0500 (Wed, 07 Apr 2010) | 1 line
Correction of unittest documentation typos and omissions
........
2010-04-11 17:43:16 -03:00
|
|
|
os.path.isdir = lambda path: True
|
|
|
|
|
|
|
|
def restore_isdir():
|
|
|
|
os.path.isdir = original_isdir
|
|
|
|
self.addCleanup(restore_isdir)
|
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
_find_tests_args = []
|
2013-11-23 09:29:23 -04:00
|
|
|
def _find_tests(start_dir, pattern, namespace=None):
|
2010-03-27 09:34:21 -03:00
|
|
|
_find_tests_args.append((start_dir, pattern))
|
|
|
|
return ['tests']
|
|
|
|
loader._find_tests = _find_tests
|
|
|
|
loader.suiteClass = str
|
|
|
|
|
|
|
|
suite = loader.discover('/foo/bar/baz', 'pattern', '/foo/bar')
|
|
|
|
|
Merged revisions 79464,79471,79623,79626,79630,79632,79643,79648-79649,79679,79685,79711,79761,79774,79777,79792-79794,79877,79898-79900 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r79464 | michael.foord | 2010-03-27 07:55:19 -0500 (Sat, 27 Mar 2010) | 1 line
A fix for running unittest tests on platforms without the audioop module (e.g. jython and IronPython)
........
r79471 | michael.foord | 2010-03-27 14:10:11 -0500 (Sat, 27 Mar 2010) | 4 lines
Addition of delta keyword argument to unittest.TestCase.assertAlmostEquals and assertNotAlmostEquals
This allows the comparison of objects by specifying a maximum difference; this includes the comparing of non-numeric objects that don't support rounding.
........
r79623 | michael.foord | 2010-04-02 16:42:47 -0500 (Fri, 02 Apr 2010) | 1 line
Addition of -b command line option to unittest for buffering stdout and stderr during test runs.
........
r79626 | michael.foord | 2010-04-02 17:08:29 -0500 (Fri, 02 Apr 2010) | 1 line
TestResult stores original sys.stdout and tests no longer use sys.__stdout__ (etc) in tests for unittest -b command line option
........
r79630 | michael.foord | 2010-04-02 17:30:56 -0500 (Fri, 02 Apr 2010) | 1 line
unittest tests no longer replace the sys.stdout put in place by regrtest
........
r79632 | michael.foord | 2010-04-02 17:55:59 -0500 (Fri, 02 Apr 2010) | 1 line
Issue #8038: Addition of unittest.TestCase.assertNotRegexpMatches
........
r79643 | michael.foord | 2010-04-02 20:15:21 -0500 (Fri, 02 Apr 2010) | 1 line
Support dotted module names for test discovery paths in unittest. Issue 8038.
........
r79648 | michael.foord | 2010-04-02 21:21:39 -0500 (Fri, 02 Apr 2010) | 1 line
Cross platform unittest.TestResult newline handling when buffering stdout / stderr.
........
r79649 | michael.foord | 2010-04-02 21:33:55 -0500 (Fri, 02 Apr 2010) | 1 line
Another attempt at a fix for unittest.test.test_result for windows line endings
........
r79679 | michael.foord | 2010-04-03 09:52:18 -0500 (Sat, 03 Apr 2010) | 1 line
Adding -b command line option to the unittest usage message.
........
r79685 | michael.foord | 2010-04-03 10:20:00 -0500 (Sat, 03 Apr 2010) | 1 line
Minor tweak to unittest command line usage message
........
r79711 | michael.foord | 2010-04-03 12:03:11 -0500 (Sat, 03 Apr 2010) | 1 line
Documenting new features in unittest
........
r79761 | michael.foord | 2010-04-04 17:41:54 -0500 (Sun, 04 Apr 2010) | 1 line
unittest documentation formatting changes
........
r79774 | michael.foord | 2010-04-04 18:28:44 -0500 (Sun, 04 Apr 2010) | 1 line
Adding documentation for new unittest.main() parameters
........
r79777 | michael.foord | 2010-04-04 19:39:50 -0500 (Sun, 04 Apr 2010) | 1 line
Document signal handling functions in unittest.rst
........
r79792 | michael.foord | 2010-04-05 05:26:26 -0500 (Mon, 05 Apr 2010) | 1 line
Documentation fixes for unittest
........
r79793 | michael.foord | 2010-04-05 05:28:27 -0500 (Mon, 05 Apr 2010) | 1 line
Furterh documentation fix for unittest.rst
........
r79794 | michael.foord | 2010-04-05 05:30:14 -0500 (Mon, 05 Apr 2010) | 1 line
Further documentation fix for unittest.rst
........
r79877 | michael.foord | 2010-04-06 18:18:16 -0500 (Tue, 06 Apr 2010) | 1 line
Fix module directory finding logic for dotted paths in unittest test discovery.
........
r79898 | michael.foord | 2010-04-07 18:04:22 -0500 (Wed, 07 Apr 2010) | 1 line
unittest.result.TestResult does not create its buffers until they're used. It uses StringIO not cStringIO. Issue 8333.
........
r79899 | michael.foord | 2010-04-07 19:04:24 -0500 (Wed, 07 Apr 2010) | 1 line
Switch regrtest to use StringIO instead of cStringIO for test_multiprocessing on Windows. Issue 8333.
........
r79900 | michael.foord | 2010-04-07 23:33:20 -0500 (Wed, 07 Apr 2010) | 1 line
Correction of unittest documentation typos and omissions
........
2010-04-11 17:43:16 -03:00
|
|
|
top_level_dir = os.path.abspath('/foo/bar')
|
|
|
|
start_dir = os.path.abspath('/foo/bar/baz')
|
2010-03-27 09:34:21 -03:00
|
|
|
self.assertEqual(suite, "['tests']")
|
|
|
|
self.assertEqual(loader._top_level_dir, top_level_dir)
|
|
|
|
self.assertEqual(_find_tests_args, [(start_dir, 'pattern')])
|
|
|
|
self.assertIn(top_level_dir, sys.path)
|
|
|
|
|
2013-03-01 08:47:50 -04:00
|
|
|
def setup_import_issue_tests(self, fakefile):
|
2010-03-27 09:34:21 -03:00
|
|
|
listdir = os.listdir
|
2013-03-01 08:47:50 -04:00
|
|
|
os.listdir = lambda _: [fakefile]
|
2010-03-27 09:34:21 -03:00
|
|
|
isfile = os.path.isfile
|
|
|
|
os.path.isfile = lambda _: True
|
|
|
|
orig_sys_path = sys.path[:]
|
|
|
|
def restore():
|
|
|
|
os.path.isfile = isfile
|
|
|
|
os.listdir = listdir
|
|
|
|
sys.path[:] = orig_sys_path
|
|
|
|
self.addCleanup(restore)
|
|
|
|
|
2014-09-08 15:21:37 -03:00
|
|
|
def setup_import_issue_package_tests(self, vfs):
|
|
|
|
self.addCleanup(setattr, os, 'listdir', os.listdir)
|
|
|
|
self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)
|
|
|
|
self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)
|
|
|
|
self.addCleanup(sys.path.__setitem__, slice(None), list(sys.path))
|
|
|
|
def list_dir(path):
|
|
|
|
return list(vfs[path])
|
|
|
|
os.listdir = list_dir
|
|
|
|
os.path.isdir = lambda path: not path.endswith('.py')
|
|
|
|
os.path.isfile = lambda path: path.endswith('.py')
|
|
|
|
|
2013-03-01 08:47:50 -04:00
|
|
|
def test_discover_with_modules_that_fail_to_import(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
self.setup_import_issue_tests('test_this_does_not_exist.py')
|
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
suite = loader.discover('.')
|
|
|
|
self.assertIn(os.getcwd(), sys.path)
|
|
|
|
self.assertEqual(suite.countTestCases(), 1)
|
|
|
|
test = list(list(suite)[0])[0] # extract test from suite
|
|
|
|
|
|
|
|
with self.assertRaises(ImportError):
|
|
|
|
test.test_this_does_not_exist()
|
|
|
|
|
2014-09-08 15:21:37 -03:00
|
|
|
def test_discover_with_init_modules_that_fail_to_import(self):
|
2014-09-29 22:54:28 -03:00
|
|
|
vfs = {abspath('/foo'): ['my_package'],
|
|
|
|
abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
|
2014-09-08 15:21:37 -03:00
|
|
|
self.setup_import_issue_package_tests(vfs)
|
|
|
|
import_calls = []
|
|
|
|
def _get_module_from_name(name):
|
|
|
|
import_calls.append(name)
|
|
|
|
raise ImportError("Cannot import Name")
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
loader._get_module_from_name = _get_module_from_name
|
2014-09-29 22:54:28 -03:00
|
|
|
suite = loader.discover(abspath('/foo'))
|
2014-09-08 15:21:37 -03:00
|
|
|
|
2014-09-29 22:54:28 -03:00
|
|
|
self.assertIn(abspath('/foo'), sys.path)
|
2014-09-08 15:21:37 -03:00
|
|
|
self.assertEqual(suite.countTestCases(), 1)
|
|
|
|
test = list(list(suite)[0])[0] # extract test from suite
|
|
|
|
with self.assertRaises(ImportError):
|
|
|
|
test.my_package()
|
|
|
|
self.assertEqual(import_calls, ['my_package'])
|
|
|
|
|
2013-03-01 08:47:50 -04:00
|
|
|
def test_discover_with_module_that_raises_SkipTest_on_import(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
def _get_module_from_name(name):
|
|
|
|
raise unittest.SkipTest('skipperoo')
|
|
|
|
loader._get_module_from_name = _get_module_from_name
|
|
|
|
|
|
|
|
self.setup_import_issue_tests('test_skip_dummy.py')
|
|
|
|
|
|
|
|
suite = loader.discover('.')
|
|
|
|
self.assertEqual(suite.countTestCases(), 1)
|
|
|
|
|
|
|
|
result = unittest.TestResult()
|
|
|
|
suite.run(result)
|
|
|
|
self.assertEqual(len(result.skipped), 1)
|
|
|
|
|
2014-09-08 15:21:37 -03:00
|
|
|
def test_discover_with_init_module_that_raises_SkipTest_on_import(self):
|
2014-09-29 22:54:28 -03:00
|
|
|
vfs = {abspath('/foo'): ['my_package'],
|
|
|
|
abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
|
2014-09-08 15:21:37 -03:00
|
|
|
self.setup_import_issue_package_tests(vfs)
|
|
|
|
import_calls = []
|
|
|
|
def _get_module_from_name(name):
|
|
|
|
import_calls.append(name)
|
|
|
|
raise unittest.SkipTest('skipperoo')
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
loader._get_module_from_name = _get_module_from_name
|
2014-09-29 22:54:28 -03:00
|
|
|
suite = loader.discover(abspath('/foo'))
|
2014-09-08 15:21:37 -03:00
|
|
|
|
2014-09-29 22:54:28 -03:00
|
|
|
self.assertIn(abspath('/foo'), sys.path)
|
2014-09-08 15:21:37 -03:00
|
|
|
self.assertEqual(suite.countTestCases(), 1)
|
|
|
|
result = unittest.TestResult()
|
|
|
|
suite.run(result)
|
|
|
|
self.assertEqual(len(result.skipped), 1)
|
|
|
|
self.assertEqual(result.testsRun, 1)
|
|
|
|
self.assertEqual(import_calls, ['my_package'])
|
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
def test_command_line_handling_parseArgs(self):
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
args = []
|
2013-08-29 06:37:28 -03:00
|
|
|
program._do_discovery = args.append
|
2010-03-27 09:34:21 -03:00
|
|
|
program.parseArgs(['something', 'discover'])
|
2013-08-29 06:37:28 -03:00
|
|
|
self.assertEqual(args, [[]])
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2013-08-29 06:37:28 -03:00
|
|
|
args[:] = []
|
2010-03-27 09:34:21 -03:00
|
|
|
program.parseArgs(['something', 'discover', 'foo', 'bar'])
|
2013-08-29 06:37:28 -03:00
|
|
|
self.assertEqual(args, [['foo', 'bar']])
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2010-11-21 17:28:01 -04:00
|
|
|
def test_command_line_handling_discover_by_default(self):
|
|
|
|
program = TestableTestProgram()
|
|
|
|
|
2013-08-29 06:37:28 -03:00
|
|
|
args = []
|
|
|
|
program._do_discovery = args.append
|
2010-11-21 17:28:01 -04:00
|
|
|
program.parseArgs(['something'])
|
2013-08-29 06:37:28 -03:00
|
|
|
self.assertEqual(args, [[]])
|
|
|
|
self.assertEqual(program.verbosity, 1)
|
|
|
|
self.assertIs(program.buffer, False)
|
|
|
|
self.assertIs(program.catchbreak, False)
|
|
|
|
self.assertIs(program.failfast, False)
|
2010-11-21 17:28:01 -04:00
|
|
|
|
2010-12-18 23:59:10 -04:00
|
|
|
def test_command_line_handling_discover_by_default_with_options(self):
|
|
|
|
program = TestableTestProgram()
|
|
|
|
|
2013-08-29 06:37:28 -03:00
|
|
|
args = []
|
|
|
|
program._do_discovery = args.append
|
|
|
|
program.parseArgs(['something', '-v', '-b', '-v', '-c', '-f'])
|
|
|
|
self.assertEqual(args, [[]])
|
|
|
|
self.assertEqual(program.verbosity, 2)
|
|
|
|
self.assertIs(program.buffer, True)
|
|
|
|
self.assertIs(program.catchbreak, True)
|
|
|
|
self.assertIs(program.failfast, True)
|
2010-12-18 23:59:10 -04:00
|
|
|
|
2010-11-21 17:28:01 -04:00
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
def test_command_line_handling_do_discovery_too_many_arguments(self):
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2013-02-10 20:04:24 -04:00
|
|
|
program.testLoader = None
|
2010-03-27 09:34:21 -03:00
|
|
|
|
2013-08-29 06:37:28 -03:00
|
|
|
with support.captured_stderr() as stderr, \
|
|
|
|
self.assertRaises(SystemExit) as cm:
|
2010-03-27 09:34:21 -03:00
|
|
|
# too many args
|
|
|
|
program._do_discovery(['one', 'two', 'three', 'four'])
|
2013-08-29 06:37:28 -03:00
|
|
|
self.assertEqual(cm.exception.args, (2,))
|
|
|
|
self.assertIn('usage:', stderr.getvalue())
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
|
2013-02-10 20:04:24 -04:00
|
|
|
def test_command_line_handling_do_discovery_uses_default_loader(self):
|
|
|
|
program = object.__new__(unittest.TestProgram)
|
2013-08-29 06:37:28 -03:00
|
|
|
program._initArgParsers()
|
2013-02-10 20:04:24 -04:00
|
|
|
|
|
|
|
class Loader(object):
|
|
|
|
args = []
|
|
|
|
def discover(self, start_dir, pattern, top_level_dir):
|
|
|
|
self.args.append((start_dir, pattern, top_level_dir))
|
|
|
|
return 'tests'
|
|
|
|
|
2013-02-11 09:20:52 -04:00
|
|
|
program.testLoader = Loader()
|
2013-02-10 20:04:24 -04:00
|
|
|
program._do_discovery(['-v'])
|
|
|
|
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
|
|
|
|
|
2010-03-27 09:34:21 -03:00
|
|
|
def test_command_line_handling_do_discovery_calls_loader(self):
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
class Loader(object):
|
|
|
|
args = []
|
|
|
|
def discover(self, start_dir, pattern, top_level_dir):
|
|
|
|
self.args.append((start_dir, pattern, top_level_dir))
|
|
|
|
return 'tests'
|
|
|
|
|
|
|
|
program._do_discovery(['-v'], Loader=Loader)
|
|
|
|
self.assertEqual(program.verbosity, 2)
|
|
|
|
self.assertEqual(program.test, 'tests')
|
|
|
|
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
|
|
|
|
|
|
|
|
Loader.args = []
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 09:34:21 -03:00
|
|
|
program._do_discovery(['--verbose'], Loader=Loader)
|
|
|
|
self.assertEqual(program.test, 'tests')
|
|
|
|
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
|
|
|
|
|
|
|
|
Loader.args = []
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 09:34:21 -03:00
|
|
|
program._do_discovery([], Loader=Loader)
|
|
|
|
self.assertEqual(program.test, 'tests')
|
|
|
|
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
|
|
|
|
|
|
|
|
Loader.args = []
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 09:34:21 -03:00
|
|
|
program._do_discovery(['fish'], Loader=Loader)
|
|
|
|
self.assertEqual(program.test, 'tests')
|
|
|
|
self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
|
|
|
|
|
|
|
|
Loader.args = []
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 09:34:21 -03:00
|
|
|
program._do_discovery(['fish', 'eggs'], Loader=Loader)
|
|
|
|
self.assertEqual(program.test, 'tests')
|
|
|
|
self.assertEqual(Loader.args, [('fish', 'eggs', None)])
|
|
|
|
|
|
|
|
Loader.args = []
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 09:34:21 -03:00
|
|
|
program._do_discovery(['fish', 'eggs', 'ham'], Loader=Loader)
|
|
|
|
self.assertEqual(program.test, 'tests')
|
|
|
|
self.assertEqual(Loader.args, [('fish', 'eggs', 'ham')])
|
|
|
|
|
|
|
|
Loader.args = []
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 09:34:21 -03:00
|
|
|
program._do_discovery(['-s', 'fish'], Loader=Loader)
|
|
|
|
self.assertEqual(program.test, 'tests')
|
|
|
|
self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
|
|
|
|
|
|
|
|
Loader.args = []
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 09:34:21 -03:00
|
|
|
program._do_discovery(['-t', 'fish'], Loader=Loader)
|
|
|
|
self.assertEqual(program.test, 'tests')
|
|
|
|
self.assertEqual(Loader.args, [('.', 'test*.py', 'fish')])
|
|
|
|
|
|
|
|
Loader.args = []
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 09:34:21 -03:00
|
|
|
program._do_discovery(['-p', 'fish'], Loader=Loader)
|
|
|
|
self.assertEqual(program.test, 'tests')
|
|
|
|
self.assertEqual(Loader.args, [('.', 'fish', None)])
|
|
|
|
self.assertFalse(program.failfast)
|
2010-03-27 10:25:41 -03:00
|
|
|
self.assertFalse(program.catchbreak)
|
2010-03-27 09:34:21 -03:00
|
|
|
|
|
|
|
Loader.args = []
|
2010-11-21 17:28:01 -04:00
|
|
|
program = TestableTestProgram()
|
2010-03-27 10:25:41 -03:00
|
|
|
program._do_discovery(['-p', 'eggs', '-s', 'fish', '-v', '-f', '-c'],
|
|
|
|
Loader=Loader)
|
2010-03-27 09:34:21 -03:00
|
|
|
self.assertEqual(program.test, 'tests')
|
|
|
|
self.assertEqual(Loader.args, [('fish', 'eggs', None)])
|
|
|
|
self.assertEqual(program.verbosity, 2)
|
|
|
|
self.assertTrue(program.failfast)
|
2010-03-27 10:25:41 -03:00
|
|
|
self.assertTrue(program.catchbreak)
|
|
|
|
|
2013-10-23 14:11:29 -03:00
|
|
|
def setup_module_clash(self):
|
2010-05-07 20:42:40 -03:00
|
|
|
class Module(object):
|
|
|
|
__file__ = 'bar/foo.py'
|
|
|
|
sys.modules['foo'] = Module
|
|
|
|
full_path = os.path.abspath('foo')
|
|
|
|
original_listdir = os.listdir
|
|
|
|
original_isfile = os.path.isfile
|
|
|
|
original_isdir = os.path.isdir
|
|
|
|
|
|
|
|
def cleanup():
|
|
|
|
os.listdir = original_listdir
|
|
|
|
os.path.isfile = original_isfile
|
|
|
|
os.path.isdir = original_isdir
|
|
|
|
del sys.modules['foo']
|
|
|
|
if full_path in sys.path:
|
|
|
|
sys.path.remove(full_path)
|
|
|
|
self.addCleanup(cleanup)
|
|
|
|
|
|
|
|
def listdir(_):
|
|
|
|
return ['foo.py']
|
|
|
|
def isfile(_):
|
|
|
|
return True
|
|
|
|
def isdir(_):
|
|
|
|
return True
|
|
|
|
os.listdir = listdir
|
|
|
|
os.path.isfile = isfile
|
|
|
|
os.path.isdir = isdir
|
2013-10-23 14:11:29 -03:00
|
|
|
return full_path
|
2010-05-07 20:42:40 -03:00
|
|
|
|
2013-10-23 14:11:29 -03:00
|
|
|
def test_detect_module_clash(self):
|
|
|
|
full_path = self.setup_module_clash()
|
2010-05-07 20:42:40 -03:00
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
mod_dir = os.path.abspath('bar')
|
|
|
|
expected_dir = os.path.abspath('foo')
|
|
|
|
msg = re.escape(r"'foo' module incorrectly imported from %r. Expected %r. "
|
|
|
|
"Is this module globally installed?" % (mod_dir, expected_dir))
|
2010-11-30 22:32:32 -04:00
|
|
|
self.assertRaisesRegex(
|
2010-05-07 20:42:40 -03:00
|
|
|
ImportError, '^%s$' % msg, loader.discover,
|
|
|
|
start_dir='foo', pattern='foo.py'
|
|
|
|
)
|
|
|
|
self.assertEqual(sys.path[0], full_path)
|
|
|
|
|
2013-10-23 14:11:29 -03:00
|
|
|
def test_module_symlink_ok(self):
|
|
|
|
full_path = self.setup_module_clash()
|
|
|
|
|
|
|
|
original_realpath = os.path.realpath
|
|
|
|
|
|
|
|
mod_dir = os.path.abspath('bar')
|
|
|
|
expected_dir = os.path.abspath('foo')
|
|
|
|
|
|
|
|
def cleanup():
|
|
|
|
os.path.realpath = original_realpath
|
|
|
|
self.addCleanup(cleanup)
|
|
|
|
|
|
|
|
def realpath(path):
|
|
|
|
if path == os.path.join(mod_dir, 'foo.py'):
|
|
|
|
return os.path.join(expected_dir, 'foo.py')
|
|
|
|
return path
|
|
|
|
os.path.realpath = realpath
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
loader.discover(start_dir='foo', pattern='foo.py')
|
2010-03-27 10:25:41 -03:00
|
|
|
|
2010-05-08 10:23:31 -03:00
|
|
|
def test_discovery_from_dotted_path(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
tests = [self]
|
|
|
|
expectedPath = os.path.abspath(os.path.dirname(unittest.test.__file__))
|
|
|
|
|
|
|
|
self.wasRun = False
|
2013-11-23 09:29:23 -04:00
|
|
|
def _find_tests(start_dir, pattern, namespace=None):
|
2010-05-08 10:23:31 -03:00
|
|
|
self.wasRun = True
|
|
|
|
self.assertEqual(start_dir, expectedPath)
|
|
|
|
return tests
|
|
|
|
loader._find_tests = _find_tests
|
|
|
|
suite = loader.discover('unittest.test')
|
|
|
|
self.assertTrue(self.wasRun)
|
|
|
|
self.assertEqual(suite._tests, tests)
|
|
|
|
|
|
|
|
|
2013-11-23 09:29:23 -04:00
|
|
|
def test_discovery_from_dotted_path_builtin_modules(self):
|
|
|
|
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
listdir = os.listdir
|
|
|
|
os.listdir = lambda _: ['test_this_does_not_exist.py']
|
|
|
|
isfile = os.path.isfile
|
|
|
|
isdir = os.path.isdir
|
|
|
|
os.path.isdir = lambda _: False
|
|
|
|
orig_sys_path = sys.path[:]
|
|
|
|
def restore():
|
|
|
|
os.path.isfile = isfile
|
|
|
|
os.path.isdir = isdir
|
|
|
|
os.listdir = listdir
|
|
|
|
sys.path[:] = orig_sys_path
|
|
|
|
self.addCleanup(restore)
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError) as cm:
|
|
|
|
loader.discover('sys')
|
|
|
|
self.assertEqual(str(cm.exception),
|
|
|
|
'Can not use builtin modules '
|
|
|
|
'as dotted module names')
|
|
|
|
|
|
|
|
def test_discovery_from_dotted_namespace_packages(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
|
|
|
|
orig_import = __import__
|
|
|
|
package = types.ModuleType('package')
|
|
|
|
package.__path__ = ['/a', '/b']
|
|
|
|
package.__spec__ = types.SimpleNamespace(
|
|
|
|
loader=None,
|
|
|
|
submodule_search_locations=['/a', '/b']
|
|
|
|
)
|
|
|
|
|
|
|
|
def _import(packagename, *args, **kwargs):
|
|
|
|
sys.modules[packagename] = package
|
|
|
|
return package
|
|
|
|
|
|
|
|
def cleanup():
|
|
|
|
builtins.__import__ = orig_import
|
|
|
|
self.addCleanup(cleanup)
|
|
|
|
builtins.__import__ = _import
|
|
|
|
|
|
|
|
_find_tests_args = []
|
|
|
|
def _find_tests(start_dir, pattern, namespace=None):
|
|
|
|
_find_tests_args.append((start_dir, pattern))
|
|
|
|
return ['%s/tests' % start_dir]
|
|
|
|
|
|
|
|
loader._find_tests = _find_tests
|
|
|
|
loader.suiteClass = list
|
|
|
|
suite = loader.discover('package')
|
|
|
|
self.assertEqual(suite, ['/a/tests', '/b/tests'])
|
|
|
|
|
|
|
|
def test_discovery_failed_discovery(self):
|
|
|
|
loader = unittest.TestLoader()
|
|
|
|
package = types.ModuleType('package')
|
|
|
|
orig_import = __import__
|
|
|
|
|
|
|
|
def _import(packagename, *args, **kwargs):
|
|
|
|
sys.modules[packagename] = package
|
|
|
|
return package
|
|
|
|
|
|
|
|
def cleanup():
|
|
|
|
builtins.__import__ = orig_import
|
|
|
|
self.addCleanup(cleanup)
|
|
|
|
builtins.__import__ = _import
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError) as cm:
|
|
|
|
loader.discover('package')
|
|
|
|
self.assertEqual(str(cm.exception),
|
|
|
|
'don\'t know how to discover from {!r}'
|
|
|
|
.format(package))
|
|
|
|
|
|
|
|
|
2010-03-27 10:25:41 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|