ValueError: no such test method in : runTest

后端 未结 6 1100
时光取名叫无心
时光取名叫无心 2020-12-03 03:10

I have a test case:

class LoginTestCase(unittest.TestCase):
    ...

I\'d like to use it in a different test case:

class Edi         


        
6条回答
  •  生来不讨喜
    2020-12-03 03:44

    The confusion with "runTest" is mostly based on the fact that this works:

    class MyTest(unittest.TestCase):
        def test_001(self):
            print "ok"
    
    if __name__ == "__main__":
        unittest.main()
    

    So there is no "runTest" in that class and all of the test-functions are being called. However if you look at the base class "TestCase" (lib/python/unittest/case.py) then you will find that it has an argument "methodName" that defaults to "runTest" but it does NOT have a default implementation of "def runTest"

    class TestCase:
        def __init__(self, methodName='runTest'):
    

    The reason that unittest.main works fine is based on the fact that it does not need "runTest" - you can mimic the behaviour by creating a TestCase-subclass instance for all methods that you have in your subclass - just provide the name as the first argument:

    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)
    

提交回复
热议问题