Fixed #21206 -- Fixed test discovery without labels

Added test to verify an empty label performs discovery on the current
working directory.
This commit is contained in:
Preston Timmons 2013-12-31 11:41:34 -06:00 committed by Claude Paroz
parent fbbe7ca30c
commit 18d962f2e6
2 changed files with 28 additions and 12 deletions

View File

@ -154,10 +154,12 @@ class DiscoverRunner(object):
def is_discoverable(label): def is_discoverable(label):
""" """
Check if a test label points to a python package or file directory. Check if a test label points to a python package or file directory.
Relative labels like "." and ".." are seen as directories.
""" """
try: try:
mod = import_module(label) mod = import_module(label)
except ImportError: except (ImportError, TypeError):
pass pass
else: else:
return hasattr(mod, '__path__') return hasattr(mod, '__path__')

View File

@ -13,6 +13,18 @@ def expectedFailureIf(condition):
return lambda func: func return lambda func: func
@contextmanager
def change_cwd(directory):
current_dir = os.path.abspath(os.path.dirname(__file__))
new_dir = os.path.join(current_dir, directory)
old_cwd = os.getcwd()
os.chdir(new_dir)
try:
yield
finally:
os.chdir(old_cwd)
class DiscoverRunnerTest(TestCase): class DiscoverRunnerTest(TestCase):
def test_dotted_test_module(self): def test_dotted_test_module(self):
@ -51,23 +63,25 @@ class DiscoverRunnerTest(TestCase):
self.assertEqual(count, 1) self.assertEqual(count, 1)
def test_file_path(self): def test_file_path(self):
@contextmanager with change_cwd(".."):
def change_cwd_to_tests():
"""Change CWD to tests directory (one level up from this file)"""
current_dir = os.path.abspath(os.path.dirname(__file__))
tests_dir = os.path.join(current_dir, '..')
old_cwd = os.getcwd()
os.chdir(tests_dir)
yield
os.chdir(old_cwd)
with change_cwd_to_tests():
count = DiscoverRunner().build_suite( count = DiscoverRunner().build_suite(
["test_discovery_sample/"], ["test_discovery_sample/"],
).countTestCases() ).countTestCases()
self.assertEqual(count, 3) self.assertEqual(count, 3)
def test_empty_label(self):
"""
If the test label is empty, discovery should happen on the current
working directory.
"""
with change_cwd("."):
suite = DiscoverRunner().build_suite([])
self.assertEqual(
suite._tests[0].id().split(".")[0],
os.path.basename(os.getcwd()),
)
def test_empty_test_case(self): def test_empty_test_case(self):
count = DiscoverRunner().build_suite( count = DiscoverRunner().build_suite(
["test_discovery_sample.tests_sample.EmptyTestCase"], ["test_discovery_sample.tests_sample.EmptyTestCase"],