Michael
Michael

Reputation: 373

Selenium python how to use assertEqual method from unittest.TestCase "No such test method in <class 'unittest.case.TestCase'>"

I try to use assertEqual in usual class and can't call method from unittest.TestCase

    class MyPages(unittest.TestCase):

        @classmethod
        def setUpClass(cls):
            basetest.BaseTest().open_browser('firefox')
            basetest.BaseTest().login()


        def testCreateFolder(self):
            print "111111111"

        def testCreateFolder1(self):
            print "222222222"

        @classmethod
        def tearDownClass(cls):
            basetest.BaseTest().close_browser()

And in my BaseTest I want to make login with text assert.

    class BaseTest():

        def open_browser(self, browser):
            self.driver = config.browser[browser]
            global driver
            driver = self.driver
            driver.get(config.url)


        def login(self):
            # Go to authorisation page
            driver.find_element_by_xpath(link.header["Login_button"]).click()
            # Get text from LOGIN label and assert it with expected text
            login_text = driver.find_element_by_xpath(link.author_popup["Login_label"])
            login_text.get_attribute("text")
            print login_text.text
            unittest.TestCase().assertEqual(1, 1, "helllllllo")
            unittest.TestCase().assertEqual(login_text.text, text.author_popup["Login"],
                                "Wrong label on log in auth popup. Expected text:")

As a result I have the following:

    Error
    Traceback (most recent call last):
      File "D:\python\PD_Tests\pages\my_pages.py", line 17, in setUpClass
        basetest.BaseTest().login()
      File "D:\python\PD_Tests\tests\basetest.py", line 25, in login
        unittest.TestCase().assertEqual(1, 1, "helllllllo")
      File "C:\Python27\lib\unittest\case.py", line 191, in __init__
        (self.__class__, methodName))
    ValueError: no such test method in <class 'unittest.case.TestCase'>: runTest

Can I use assertEqual method in my method if my class is not unittest.TestCase?

Upvotes: 0

Views: 4538

Answers (2)

Tobias
Tobias

Reputation: 2561

Is there a special reason not to subclass BaseTest from unittest.TestCase?

My solution would have been to have a SeleniumTestcase class, which inherits from unittest.TestCase and provides the setUp and tearDown methods. Just make sure your config is known when your SeleniumTestcase instance is created.

Upvotes: 1

Luke Woodward
Luke Woodward

Reputation: 64959

I think there's a way to do what you want, but it's a little bit of a hack.

The constructor for the TestCase class takes a method name as a parameter, and this parameter has the default value "runTest". The docstring for this constructor reads as follows:

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

Hopefully this should explain the error message you are seeing.

If you want to create a TestCase just to use the assert methods, you can pass in the name of some other method instead, such as __str__. That will get you past the checking done in the constructor:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest import TestCase
>>> t = TestCase("__str__")
>>> t.assertEqual(3, 5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\unittest\case.py", line 511, in assertEqual
    assertion_func(first, second, msg=msg)
  File "C:\Python27\lib\unittest\case.py", line 504, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: 3 != 5
>>> t.assertEqual(3, 3)
>>>

As long as you don't try to run your TestCase, this shouldn't be a problem.

Upvotes: 1

Related Questions