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'.