The problem is that the loader from unittest runs the dir () class in the class before it is created, so it doesn't matter where you create your methods, __new__
, __init__
, setUpClass
, etc ... methods are created too late.
There are two ways around this, write your own run () method, or use a metaclass. The first means that you need to re-write the discovery that is already written in unittest. The metaclass is actually not that hard to implement. Here is what I did:
import unittest import blognodes class meta_Test_base62(type): testset = { 0: '0', 10: 'a', 61: 'Z', 62: '10', 3844: '100'} @classmethod def __prepare__(mcls, name, bases): d = dict() d['testme'] = 5 for b10, b62 in mcls.testset.items(): fname = "test_base62_value_{}".format(b10) d[fname] = mcls.build_test_base62_values(b10, b62) fname = "test_int_value_{}".format(b10) d[fname] = mcls.build_test_int_values(b10, b62) return d @classmethod def build_test_base62_values(cls, b10, b62): def f(self): target = blognodes.base62(b10) self.assertEqual(target.str(), b62) return f @classmethod def build_test_int_values(cls, b10, b62): def f(self): target = blognodes.base62(b10) self.assertEqual(target.int(), b10) return f class Test_base62(unittest.TestCase, metaclass=meta_Test_base62): def test_nothing(self): self.assertEqual(5, self.testme)
source share