4

I am trying some Python with selenium, I have some defined tests in simpleUsageUnittest.py:

import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()

#   @unittest.skip("skip test_001")
    def test_001_search_in_python_org(self):
        driver = self.driver
        driver.get("http://www.python.org")
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("selenium")
        elem.send_keys(Keys.RETURN)

#   @unittest.skip("skip test_002")
    def test_002_goole_and_stack_test_test(self):
        driver_g = self.driver
        driver_g.get("http://www.google.com")
        self.assertIn("Google", driver_g.title)
        body_g = driver_g.find_element_by_tag_name("body")
        body_g.send_keys(Keys.CONTROL + 't')
        driver_g.get("http://stackoverflow.com")
        self.assertIn("Stack", driver_g.title)

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main(warnings = 'ignore')

Alone this set is working perfectly, but then I am trying to create some suite, testTestSuite.py:

import unittest
import simpleUsageUnittest
import sys

def suite():
    testSuite = unittest.TestSuite()
    testSuite.addTest(simpleUsageUnittest.PythonOrgSearch.setUp)
    testSuite.addTest(simpleUsageUnittest.PythonOrgSearch.test_001_search_in_python_org)
    testSuite.addTest(simpleUsageUnittest.PythonOrgSearch.test_002_goole_and_stack_test_test)
    testSuite.addTest(simpleUsageUnittest.PythonOrgSearch.tearDown)
    return testSuite

if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    sys.exit(not result.wasSuccessful())

And while running this suite I encounter AttributeError: 'TextTestResult' object has no attribute 'assertIn', and since I dont exactly understand it I cannot fix it ;) If I delete assertIn lines in simpleUsageUnittest.py - then it is working again, but it is of course not what I want to do! Also Asserts in Python 2.7 not working for me example assertIn was not a big help since I am using Python 3.3.5 and Selenium 2.41.0. Can someone explain it to me? Or direct what attributes can I use to save my assert? ;)

Full trace:

C:\Users\zzz\Python\selenium_tutorial>python testTestSuite.py
Traceback (most recent call last):
  File "testTestSuite.py", line 14, in <module>
    result = unittest.TextTestRunner(verbosity=2).run(suite())
  File "C:\Python33\lib\unittest\runner.py", line 168, in run
    test(result)
  File "C:\Python33\lib\unittest\suite.py", line 67, in __call__
    return self.run(*args, **kwds)
  File "C:\Python33\lib\unittest\suite.py", line 105, in run
    test(result)
  File "C:\Users\zzz\Python\selenium_tutorial\simpleUsageUnittest.py", line
 18, in test_001_search_in_python_org
    self.assertIn("Python", driver.title)
AttributeError: 'TextTestResult' object has no attribute 'assertIn'

SOLUTION

OK, it looks like in my testTestSuite.py, while executing, TextTestRunner treats "self.asserIn" lines in simpleUsageUnittest.py as self == TextTestRunner not as self == TestCase (I dont know if I explain/understand it correctly, but it is how i see it ;)). Here is fixed testTestSuite.py:

import unittest
import simpleUsageUnittest
import sys

def suite():
    testSuite = unittest.TestSuite()
    testSuite.addTest(simpleUsageUnittest.PythonOrgSearch('test_001_search_in_python_org'))
    testSuite.addTest(simpleUsageUnittest.PythonOrgSearch('test_002_goole_and_stack_test_test'))
    return testSuite

if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    sys.exit(not result.wasSuccessful())

'setUp' and 'tearDown' are missing because they are called automatically after every 'test'.

Community
  • 1
  • 1
hegelsturm
  • 71
  • 1
  • 6
  • Can you provide a full traceback of the error? – Wilfred Hughes May 12 '14 at 09:17
  • I added Trace in question. – hegelsturm May 12 '14 at 09:27
  • According to python documentation `assertIn` is added in python3.1. Are you sure you are running python3.3 ? https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertIn – sagarchalise May 12 '14 at 09:32
  • Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. Yes I am - at least it is what i get after typing python into console. – hegelsturm May 12 '14 at 09:36
  • OK I was able to solve this (solution added to description). Many thanks for sugestions and http://stackoverflow.com/questions/1732438/run-all-unit-test-in-python-directory was helpful! Also many thanks to my friend Sebastian for help with this solution. – hegelsturm May 12 '14 at 13:08
  • I posted the solution as community wiki answer so that's clear it's resolved. – Danstahr May 12 '14 at 13:13

1 Answers1

2

SOLUTION

OK, it looks like in my testTestSuite.py, while executing, TextTestRunner treats "self.asserIn" lines in simpleUsageUnittest.py as self == TextTestRunner not as self == TestCase (I dont know if I explain/understand it correctly, but it is how i see it ;)). Here is fixed testTestSuite.py:

import unittest
import simpleUsageUnittest
import sys

def suite():
    testSuite = unittest.TestSuite()
    testSuite.addTest(simpleUsageUnittest.PythonOrgSearch('test_001_search_in_python_org'))
    testSuite.addTest(simpleUsageUnittest.PythonOrgSearch('test_002_goole_and_stack_test_test'))
    return testSuite

if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    sys.exit(not result.wasSuccessful())

'setUp' and 'tearDown' are missing because they are called automatically after every 'test'.

Danstahr
  • 4,190
  • 22
  • 38