[英]how to add tests dynamically to python unittest
I'm wanting to set up a unittest
TestCase where some of the cases are added dynamically.我想建立一个unittest
,其中一些案件动态添加的TestCase。 The methods are added as shown from my test_nothing
, but, unittest
doesn't take them into account as it runs only one test.这些方法是添加的,如我的test_nothing
所示,但是, unittest
不考虑它们,因为它只运行一个测试。 It's like I build my test_xxxx
too late and they are not seen.就像我构建我的test_xxxx
太晚了,他们没有看到。 Is setUpClass
executed too late in the game? setUpClass
在游戏中执行得太晚了吗? Should I put this in __init__
build my methods, and then call super().__init__
?我应该把它放在__init__
构建我的方法中,然后调用super().__init__
吗?
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)
Thanks.谢谢。
The issue is that the loader from unittest runs a dir() on the class before it instantiate it, therefore, it doesn't matter where you create your methods, __new__
, __init__
, setUpClass
etc... the methods get created too late.问题是 unittest 的加载器在实例化它之前在类上运行 dir() ,因此,在何处创建方法、 __new__
、 __init__
、 setUpClass
……这些方法创建得太晚了。
There are two ways to work around that, write your own run() method, or use a metaclass.有两种方法可以解决这个问题,编写自己的 run() 方法,或使用元类。 The former means you have to re-write the discovery that's already written in unittest.前者意味着您必须重新编写已经在 unittest 中编写的发现。 The metaclass isn't actually that complicated to implement.元类实际上实现起来并不复杂。 Here's 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.