我有一个测试用例:
class LoginTestCase(unittest.TestCase):
...
我想在不同的测试用例中使用它:
class EditProfileTestCase(unittest.TestCase):
def __init__(self):
self.t = LoginTestCase()
self.t.login()
这引发了:
ValueError: no such test method in <class 'LoginTest: runTest`
我查看了调用异常的单元测试代码,看起来测试不应该以这种方式编写。有没有一种标准的方法来编写你想要测试的东西,以便以后的测试可以重用它?或者有什么解决方法?
我现在向 LoginTest
添加了一个空的 runTest
方法,作为一种可疑的解决方法。
最佳答案
与“runTest”的混淆主要是基于这样的事实:
class MyTest(unittest.TestCase):
def test_001(self):
print "ok"
if __name__ == "__main__":
unittest.main()
因此,该类中没有“runTest”,并且正在调用所有测试函数。但是,如果您查看基类“TestCase”(lib/python/unittest/case.py),您会发现它有一个默认为“runTest”的参数“methodName”,但它没有“def runTest"
class TestCase:
def __init__(self, methodName='runTest'):
unittest.main 工作正常的原因在于它不需要“runTest”——您可以通过为子类中的所有方法创建一个 TestCase 子类实例来模仿该行为——只需提供名称作为第一个参数:
class MyTest(unittest.TestCase):
def test_001(self):
print "ok"
if __name__ == "__main__":
suite = unittest.TestSuite()
for method in dir(MyTest):
if method.startswith("test"):
suite.addTest(MyTest(method))
unittest.TextTestRunner().run(suite)
关于python - 值错误 : no such test method in <class 'myapp.tests.SessionTestCase' >: runTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2090479/