我有一组测试用例,它们都应该完成完全相同的测试,按照“方法 x 是否返回现有文件的名称?”。
我认为最好的方法是从他们都共享的 TestCase 派生一个基类,然后简单地将测试添加到该类中。不幸的是,测试框架仍然试图为基类运行测试,这没有任何意义。
class SharedTest(TestCase):
def x(self):
...do test...
class OneTestCase(SharedTest):
...my tests are performed, and 'SharedTest.x()'...
如果在基类的对象而不是像这样的派生类上调用它,我试图破解检查以简单地跳过测试:
class SharedTest(TestCase):
def x(self):
if type(self) != type(SharedTest()):
...do test...
else:
pass
但收到此错误:
ValueError: no such test method in <class 'tests.SharedTest'>: runTest
首先,我想要任何优雅的建议。其次,虽然我真的不想使用 type() hack,但我想了解它为什么不起作用。