我想设置一个 < code> unitest testCase >, 其中某些案例被动态添加到其中。 方法按我 < code> test_ nothing code> 所显示的添加, 但是, < code> unitest code> 没有考虑到它们, 因为它只运行一次测试。 就像我为时过晚而建立我的 < code> test_ xx code > 。 游戏中的 < code> setUpClass code > 是否执行得太晚了? 我是否应该在 < code_ init_ / code > 中设置这个方法, 然后调用 < code> super (). init_ {/code >?
import unittest
import blognodes
class Test_base62(unittest.TestCase):
testset = { 0: 0 , 10: a , 61: Z , 62: 10 , 3844: 100 }
@classmethod
def setUpClass(cls):
cls.testme = 5
print("i am the setUp function")
for d, b62 in cls.testset.items():
print("building the first set")
cls.build_test_base62_values(d, b62)
print("building the second set")
cls.build_test_int_values(d, b62)
@classmethod
def build_test_base62_values(cls, d, b62):
def f(cls):
target = blognodes.base62(d)
cls.assertEqual(target.str(), b62)
fname = "test_base62_value_{}".format(d)
setattr(cls, fname, f)
@classmethod
def build_test_int_values(cls, d, b62):
def f(cls):
target = blognodes.base62(d)
cls.assertEqual(target.int(), d)
fname = "test_int_value_{}".format(d)
setattr(cls, fname, f)
def test_nothing(self):
print("i m test nothing")
t = dir(self)
print(t)
self.assertEqual(5, self.testme)
谢谢