diff options
author | John Szakmeister <john@szakmeister.net> | 2015-01-25 06:21:43 -0500 |
---|---|---|
committer | John Szakmeister <john@szakmeister.net> | 2015-01-25 09:12:39 -0500 |
commit | d9b92adc1e5a40b10bda5bc3e86abc5bebde2bcf (patch) | |
tree | e24ef2d6f9635b6f95f07741c2434f56f5ac3927 | |
parent | 3510bda1e6fd7cb66fd6bb82f6c560991503fdee (diff) | |
download | nose-d9b92adc1e5a40b10bda5bc3e86abc5bebde2bcf.tar.gz |
Fix #875: nose doesn't collect tests when subpackage is given as arg
When a subpackage is specified on the command line (e.g., `nosetests
foo.bar`), it should pick up all the tests below the subpackage.
The root of the problem came from how we resolved a filename for a
package. We should really point to the package directory, instead of
providing the path to the `__init__.py` file. Otherwise, we only end up
attempting to select modules from the `__init__.py` instead of the whole
tree underneath. We don't want to check for a path ending in
`__init__.py` in `loadTestsFromModule()` because then we select the
whole subtree of tests when `foo/bar/__init__.py` is specified when only
tests in the __init__.py should be selected.
-rw-r--r-- | functional_tests/support/package2/test_pak/__init__.py | 1 | ||||
-rw-r--r-- | functional_tests/support/package2/test_pak/test_sub/__init__.py | 3 | ||||
-rw-r--r-- | functional_tests/test_loader.py | 59 | ||||
-rw-r--r-- | functional_tests/test_program.py | 6 | ||||
-rw-r--r-- | nose/util.py | 10 |
5 files changed, 67 insertions, 12 deletions
diff --git a/functional_tests/support/package2/test_pak/__init__.py b/functional_tests/support/package2/test_pak/__init__.py index 3a05d5f..b65d6c0 100644 --- a/functional_tests/support/package2/test_pak/__init__.py +++ b/functional_tests/support/package2/test_pak/__init__.py @@ -5,7 +5,6 @@ def setup(): # print "SETUP CALLED", state, id(state) state.append('test_pak.setup') - def teardown(): # print "TEARDOWN CALLED", state, id(state) state.append('test_pak.teardown') diff --git a/functional_tests/support/package2/test_pak/test_sub/__init__.py b/functional_tests/support/package2/test_pak/test_sub/__init__.py index a3d051a..7346091 100644 --- a/functional_tests/support/package2/test_pak/test_sub/__init__.py +++ b/functional_tests/support/package2/test_pak/test_sub/__init__.py @@ -7,3 +7,6 @@ def setup(): def teardown(): # print "SUB teardown called", state, id(state) state.append('test_pak.test_sub.teardown') + +def test_sub_init(): + state.append('test_pak.test_sub.test_sub_init') diff --git a/functional_tests/test_loader.py b/functional_tests/test_loader.py index 6f73559..81aaa7b 100644 --- a/functional_tests/test_loader.py +++ b/functional_tests/test_loader.py @@ -75,7 +75,7 @@ class TestNoseTestLoader(unittest.TestCase): assert not res.errors, res.errors assert not res.failures, res.failures - self.assertEqual(res.testsRun, 5) + self.assertEqual(res.testsRun, 6) # Expected order of calls expect = ['test_pak.setup', @@ -84,6 +84,7 @@ class TestNoseTestLoader(unittest.TestCase): 'test_pak.test_mod.test_minus', 'test_pak.test_mod.teardown', 'test_pak.test_sub.setup', + 'test_pak.test_sub.test_sub_init', 'test_pak.test_sub.test_mod.setup', 'test_pak.test_sub.test_mod.TestMaths.setup_class', 'test_pak.test_sub.test_mod.TestMaths.setup', @@ -414,8 +415,60 @@ class TestNoseTestLoader(unittest.TestCase): print res.errors self.assertEqual(len(res.errors), 1) assert 'raise Exception("pow")' in res.errors[0][1] - - + + def test_load_from_file(self): + res = unittest.TestResult() + wd = os.path.join(support, 'package2') + l = loader.TestLoader(workingDir=wd) + suite = l.loadTestsFromName('test_pak/test_sub/__init__.py') + suite(res) + + assert 'test_pak' in sys.modules, \ + "Context did not load test_pak" + m = sys.modules['test_pak'] + print "test pak state", m.state + expect = ['test_pak.setup', + 'test_pak.test_sub.setup', + 'test_pak.test_sub.test_sub_init', + 'test_pak.test_sub.teardown', + 'test_pak.teardown'] + self.assertEqual(len(m.state), len(expect)) + for item in m.state: + self.assertEqual(item, expect.pop(0)) + + + def test_load_from_sub_package(self): + res = unittest.TestResult() + wd = os.path.join(support, 'package2') + l = loader.TestLoader(workingDir=wd) + suite = l.loadTestsFromName('test_pak.test_sub') + suite(res) + + assert 'test_pak' in sys.modules, \ + "Context did not load test_pak" + m = sys.modules['test_pak'] + print "test pak state", m.state + expect = ['test_pak.setup', + 'test_pak.test_sub.setup', + 'test_pak.test_sub.test_sub_init', + 'test_pak.test_sub.test_mod.setup', + 'test_pak.test_sub.test_mod.TestMaths.setup_class', + 'test_pak.test_sub.test_mod.TestMaths.setup', + 'test_pak.test_sub.test_mod.TestMaths.test_div', + 'test_pak.test_sub.test_mod.TestMaths.teardown', + 'test_pak.test_sub.test_mod.TestMaths.setup', + 'test_pak.test_sub.test_mod.TestMaths.test_two_two', + 'test_pak.test_sub.test_mod.TestMaths.teardown', + 'test_pak.test_sub.test_mod.TestMaths.teardown_class', + 'test_pak.test_sub.test_mod.test', + 'test_pak.test_sub.test_mod.teardown', + 'test_pak.test_sub.teardown', + 'test_pak.teardown'] + self.assertEqual(len(m.state), len(expect)) + for item in m.state: + self.assertEqual(item, expect.pop(0)) + + # used for comparing lists def diff(a, b): return '\n' + '\n'.join([ l for l in ndiff(a, b) diff --git a/functional_tests/test_program.py b/functional_tests/test_program.py index 6ff600d..27c245f 100644 --- a/functional_tests/test_program.py +++ b/functional_tests/test_program.py @@ -56,8 +56,8 @@ class TestTestProgram(unittest.TestCase): exit=False) res = runner.result print stream.getvalue() - self.assertEqual(res.testsRun, 5, - "Expected to run 5 tests, ran %s" % res.testsRun) + self.assertEqual(res.testsRun, 6, + "Expected to run 6 tests, ran %s" % res.testsRun) assert res.wasSuccessful() assert not res.errors assert not res.failures @@ -165,7 +165,7 @@ class TestTestProgram(unittest.TestCase): exit=False) res = runner.result print stream.getvalue() - self.assertEqual(res.testsRun, 7) + self.assertEqual(res.testsRun, 8) def test_illegal_packages_not_selected(self): stream = StringIO() diff --git a/nose/util.py b/nose/util.py index e6f735e..efab8fe 100644 --- a/nose/util.py +++ b/nose/util.py @@ -221,11 +221,11 @@ def getfilename(package, relativeTo=None): if relativeTo is None: relativeTo = os.getcwd() path = os.path.join(relativeTo, os.sep.join(package.split('.'))) - suffixes = ('/__init__.py', '.py') - for suffix in suffixes: - filename = path + suffix - if os.path.exists(filename): - return filename + if os.path.exists(path + '/__init__.py'): + return path + filename = path + '.py' + if os.path.exists(filename): + return filename return None |