ali*_*noi 5 python unit-testing
我的语言是python 3,我试图了解如何使用相同的测试集来测试相同算法的不同实现.
到目前为止,我正在研究内置unittest函数,我的印象是我必须创建一个类层次结构:一个继承自unittest.TestCase的类,并实现所有实际测试以及来自其类的每个测试特定的几个后代使用来自父级的测试实现.
但是,这只是我对它应该是什么样子的看法.您能告诉我如何实际使用同一组测试来测试实现相同算法的不同功能吗?
我通过在不继承的类中编写测试TestCase,然后编写多个继承自测试的测试类来完成此TestCase 操作,但要么具有要实例化的类的工厂方法或类属性:
class TestAnyImplementation:
def testThis(self):
obj = getImpl() # call to factory method
self.assertTrue(obj.isEmpty())
class TestFooImplementation(TestAnyImplementation,TestCase):
def getImpl(self):
return Foo()
class TestBarImplementation(TestAnyImplementation,TestCase):
def getImpl(self):
return Bar()
Run Code Online (Sandbox Code Playgroud)