开发者

can't instantiate child classes of unittest.Testcase in python

i'm iterating over a text file.

each line in the file text file is the name of a 开发者_开发技巧test.

i am trying to instantiate the test class but i keep getting this error:

ValueError: no such test method in <class 'login_to_blog'>: runTest

the code where i'm doing that is here:

    test_name = line.replace("\n", "") #name of test file, class, and method _must_ be shared.
    module = __import__(test_name)
    test_class = getattr(module, test_name)
    suite.addTest(test_class())

here is login_to_blog:

from selenium import selenium
import unittest, time, re

class login_to_blog(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://blog/")
        self.selenium.start()

    def test_login_to_blog(self):
        sel = self.selenium
        sel.open("/")
        sel.type("signin_username", "jim")
        sel.type("signin_password", "jones")
        sel.click("//input[@value='Signin']")
        sel.wait_for_page_to_load("30000")
        try: self.failUnless(sel.is_text_present("your blog posts"))
        except AssertionError, e: self.verificationErrors.append(str(e))

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

it's important to note that these tests run by them selves successfully via command line.

any idea how i can instantiate them and run them manually from within python code?


Looking at the PyUnit Suite Documentation, it says:

When creating an instance we must specify the test method it is to run. We do this by passing the method name in the constructor:

    defaultSizeTestCase = WidgetTestCase("testDefaultSize")
    resizeTestCase = WidgetTestCase("testResize")

Further down, I think the thing you're looking for is:

Since it is a common pattern to create a TestCase subclass with many similarly named test functions, there is a convenience function called makeSuite provided in the unittest module that constructs a test suite that comprises all of the test cases in a test case class:-

   suite = unittest.makeSuite(WidgetTestCase,'test')

So you want:

suite = unittest.makeSuite(test_class, 'test')
result = unittest.TestResult()
suite.run(result)

or something like that.


Many people expect that they can create a testsuite and that they can add a full class based on unittest.TestCase and it will automatically run all "test*"-functions in it. That's because unittest.main() will do so. In reality however, each TestCase-class will only call one single method - have a look at the source code for unittest.TestCase at lib/python/unittest/case.py

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

and that's where the error comes from as the base class TestCase does not provide a default implementation of "def runTest". If you want to mimic the behaviour of unitest.main then you need to create one instance of your test class FOR EACH method that you want to get executed

test_name = line.replace("\n", "") #name of test file, class, and method _must_ be shared.
module = __import__(test_name)
test_class = getattr(module, test_name)
for method in dir(test_class):
    if method.startswith("test"):
        suite.addTest(test_class(method))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜