Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 611
tipu Avatar asked Feb 14 '11 02:02

tipu


People also ask

How do you write a unittest case in Python?

For example, we named the file for unit-testing as Basic_Test.py . So the command to run python unittest will be: $python3. 6 -m unittest Basic_Test. Testing If you want to see the verbose, then the command will be; $python3.

What is the use of setUp () method in test case class?

The setUp() and tearDown() methods allow you to define instructions that will be executed before and after each test method.

How do I raise an exception in unittest Python?

assertRaises(exception, callable, *args, **kwds) Test that an exception (first argument) is raised when a function is called with any positional or keyword arguments. The test passes if the expected exception is raised, is an error if another exception is raised, or fails if no exception is raised.


2 Answers

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.

like image 78
Mikel Avatar answered Sep 28 '22 04:09

Mikel


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))
like image 35
Guido U. Draheim Avatar answered Sep 28 '22 03:09

Guido U. Draheim