diff options
author | Kumar McMillan <kumar.mcmillan@gmail.com> | 2008-03-06 15:24:27 +0000 |
---|---|---|
committer | Kumar McMillan <kumar.mcmillan@gmail.com> | 2008-03-06 15:24:27 +0000 |
commit | 97f42abc29168cb15a112f73925bd04528131728 (patch) | |
tree | 3276849ed440287708f8c79da769f2bef6fbff9e | |
parent | 86adc5307b61690bab311f1ca5840a6bdf502da7 (diff) | |
download | nose-97f42abc29168cb15a112f73925bd04528131728.tar.gz |
merged Ticket-153 branch into trunk, -r433:HEAD . This fixes bugs in metaclass support when nose is discovering tests.
-rw-r--r-- | nose/util.py | 3 | ||||
-rw-r--r-- | unit_tests/test_cases.py | 16 | ||||
-rw-r--r-- | unit_tests/test_loader.py | 26 | ||||
-rw-r--r-- | unit_tests/test_selector.py | 6 | ||||
-rw-r--r-- | unit_tests/test_utils.py | 27 |
5 files changed, 77 insertions, 1 deletions
diff --git a/nose/util.py b/nose/util.py index f0bdd91..90b0e14 100644 --- a/nose/util.py +++ b/nose/util.py @@ -109,7 +109,8 @@ def isclass(obj): """Is obj a class? inspect's isclass is too liberal and returns True for objects that can't be subclasses of anything. """ - return type(obj) in class_types + obj_type = type(obj) + return obj_type in class_types or issubclass(obj_type, type) def isgenerator(func): diff --git a/unit_tests/test_cases.py b/unit_tests/test_cases.py index adc1a6c..af91f05 100644 --- a/unit_tests/test_cases.py +++ b/unit_tests/test_cases.py @@ -31,6 +31,22 @@ class TestNoseCases(unittest.TestCase): case(res) assert a[0] == 1 + def test_method_test_case_with_metaclass(self): + res = unittest.TestResult() + + class TestType(type): + def __new__(cls, name, bases, dct): + return type.__new__(cls, name, bases, dct) + a = [] + class TestClass(object): + __metaclass__ = TestType + def test_func(self, a=a): + a.append(1) + + case = nose.case.MethodTestCase(TestClass.test_func) + case(res) + assert a[0] == 1 + def test_method_test_case_fixtures(self): res = unittest.TestResult() called = [] diff --git a/unit_tests/test_loader.py b/unit_tests/test_loader.py index 2f6e3e5..08f0686 100644 --- a/unit_tests/test_loader.py +++ b/unit_tests/test_loader.py @@ -32,6 +32,8 @@ def mods(): '/package/subpackage/__init__.py') M['test_module_with_generators'] = imp.new_module( 'test_module_with_generators') + M['test_module_with_metaclass_tests'] = imp.new_module( + 'test_module_with_metaclass_tests') # a unittest testcase subclass class TC(unittest.TestCase): @@ -41,6 +43,17 @@ def mods(): class TC2(unittest.TestCase): def runTest(self): pass + + # test class that uses a metaclass + class TCType(type): + def __new__(cls, name, bases, dct): + return type.__new__(cls, name, bases, dct) + class TestMetaclassed(object): + __metaclass__ = TCType + def test_one(self): + pass + def test_two(self): + pass # test function def test_func(): @@ -110,8 +123,12 @@ def mods(): test_func_generator_name.__module__ = 'test_module_with_generators' test_func_generator.__module__ = 'test_module_with_generators' try_odd.__module__ = 'test_module_with_generators' + M['test_module_with_metaclass_tests'].TestMetaclassed = TestMetaclassed + TestMetaclassed.__module__ = 'test_module_with_metaclass_tests' del TC del TC2 + del TestMetaclassed + # del TCType del test_func del TestClass del test_func_generator @@ -399,6 +416,15 @@ class TestTestLoader(unittest.TestCase): print suite tests = [t for t in suite] assert len(tests) == 0, "Expected no tests, got %s" % tests + + def test_load_metaclass_customized_classes(self): + print "load metaclass-customized classes" + test_module_with_generators = M['test_module_with_metaclass_tests'] + l = self.l + suite = l.loadTestsFromModule(test_module_with_generators) + tc = [t for t in suite][0] + tc_methods = [m for m in tc] + self.assertEqual(len(tc_methods), 2) def test_load_generators(self): print "load generators" diff --git a/unit_tests/test_selector.py b/unit_tests/test_selector.py index ad0f4cc..f09f729 100644 --- a/unit_tests/test_selector.py +++ b/unit_tests/test_selector.py @@ -52,11 +52,17 @@ class TestSelector(unittest.TestCase): pass class TestMe: pass + class TestType(type): + def __new__(cls, name, bases, dct): + return type.__new__(cls, name, bases, dct) + class TestClass(object): + __metaclass__ = TestType s = Selector(Config()) assert not s.wantClass(Foo) assert s.wantClass(Bar) assert s.wantClass(TestMe) + assert s.wantClass(TestClass) TestMe.__test__ = False assert not s.wantClass(TestMe), "Failed to respect __test__ = False" diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py index f982cc2..ede573a 100644 --- a/unit_tests/test_utils.py +++ b/unit_tests/test_utils.py @@ -99,6 +99,33 @@ class TestUtils(unittest.TestCase): (me, __name__, 'baz')) self.assertEqual(test_address(foo_mtc), (me, __name__, 'Foo.bar')) + + def test_isclass_detects_classes(self): + class TC(unittest.TestCase): + pass + class TC_Classic: + pass + class TC_object(object): + pass + # issue153 -- was not detecting custom typed classes... + class TCType(type): + pass + class TC_custom_type(object): + __metaclass__ = TCType + class TC_unittest_custom_type(unittest.TestCase): + __metaclass__ = TCType + + assert util.isclass(TC), "failed to detect %s as class" % TC + assert util.isclass(TC_Classic), "failed to detect %s as class" % TC_Classic + assert util.isclass(TC_object), "failed to detect %s as class" % TC_object + assert util.isclass(TC_custom_type), "failed to detect %s as class" % TC_custom_type + assert util.isclass(TC_unittest_custom_type), "failed to detect %s as class" % TC_unittest_custom_type + + def test_isclass_ignores_nonclass_things(self): + anint = 1 + adict = {} + assert not util.isclass(anint), "should have ignored %s" % type(anint) + assert not util.isclass(adict), "should have ignored %s" % type(adict) def test_tolist(self): tolist = util.tolist |