Cannot create unittest.Testcase child classes in python

I repeat the text file.

each line in the text file of the file is the name of the test.

I am trying to instantiate a testing class, but I keep getting this error:

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

The code where I do this is:

  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 is important to note that these tests are successfully performed by them through the command line.

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

+4
source share
2 answers

Looking at the PyUnit Suite Documentation , he says:

When creating an instance, we must specify the testing method that it must execute. We do this by passing the method name in the constructor:

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

Next, I think you are looking for:

Since this is a common template for creating a subclass of TestCase with many similarly named test functions, the unittest module provides a convenience function called makeSuite, which creates a test suite that contains all the test cases in the 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.

+8
source

Many people expect that they can create testuite and that they can add a complete class based on unittest.TestCase, and it will automatically run all the functions of "test *". This is because unittest.main () will do this. In reality, however, each TestCase class will call only one method - look at the source code of unittest.TestCase at lib / python / unittest / case.py

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

and that where the error occurs due to the fact that the base class TestCase does not provide a standard implementation of "def runTest". If you want to emulate the behavior of unitest.main, you need to create one instance of your FOR EACH test class that you want to execute.

 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)) 
+1
source

Source: https://habr.com/ru/post/1339597/


All Articles