From 18d962f2e6cc8829d60d6f6dfb3ee3855fa5362e Mon Sep 17 00:00:00 2001 From: Preston Timmons Date: Tue, 31 Dec 2013 11:41:34 -0600 Subject: [PATCH] Fixed #21206 -- Fixed test discovery without labels Added test to verify an empty label performs discovery on the current working directory. --- django/test/runner.py | 4 ++- tests/test_runner/test_discover_runner.py | 36 ++++++++++++++++------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/django/test/runner.py b/django/test/runner.py index d8994d5ec2..d3c53ffc44 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -154,10 +154,12 @@ class DiscoverRunner(object): def is_discoverable(label): """ Check if a test label points to a python package or file directory. + + Relative labels like "." and ".." are seen as directories. """ try: mod = import_module(label) - except ImportError: + except (ImportError, TypeError): pass else: return hasattr(mod, '__path__') diff --git a/tests/test_runner/test_discover_runner.py b/tests/test_runner/test_discover_runner.py index fd68949678..0b723e3243 100644 --- a/tests/test_runner/test_discover_runner.py +++ b/tests/test_runner/test_discover_runner.py @@ -13,6 +13,18 @@ def expectedFailureIf(condition): 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): def test_dotted_test_module(self): @@ -51,23 +63,25 @@ class DiscoverRunnerTest(TestCase): self.assertEqual(count, 1) def test_file_path(self): - @contextmanager - 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(): + with change_cwd(".."): count = DiscoverRunner().build_suite( ["test_discovery_sample/"], ).countTestCases() 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): count = DiscoverRunner().build_suite( ["test_discovery_sample.tests_sample.EmptyTestCase"],