AttributeError:'TextTestResult'对象没有属性'assertIn'

时间:2014-05-12 09:13:19

标签: python selenium python-3.x selenium-webdriver

我正在尝试使用selenium的Python,我在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')

这一套完全正常,但后来我试图创建一些套件,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())

在运行这个套件时我遇到了AttributeError:'TextTestResult'对象没有属性'assertIn',因为我不完全理解它我无法修复它;)如果我删除simpleUsageUnittest.py中的assertIn行 - 那么它正在工作再次,但它当然不是我想做的!另外Asserts in Python 2.7 not working for me example assertIn也没有什么大帮助,因为我使用的是Python 3.3.5和Selenium 2.41.0。有人可以向我解释一下吗?或者指导我可以用什么属性来保存我的断言? ;)

完整追踪:

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'

好吧,看起来在我的testTestSuite.py中,在执行时,TextTestRunner将simpleUsageUnittest.py中的“self.asserIn”行视为self == TextTestRunner而不是self == TestCase(我不知道我是否解释/理解它正确,但这是我看到它;))。这是固定的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'和'tearDown'缺失,因为每次'测试'后都会自动调用它们。

1 个答案:

答案 0 :(得分:2)

<强>解

好吧,看起来在我的testTestSuite.py中,在执行时,TextTestRunner会对待&#34; self.asserIn&#34; simpleUsageUnittest.py中的行为self == TextTestRunner而不是self == TestCase(我不知道我是否正确解释/理解它,但这是我看到它的方式;))。这是固定的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())

&#39;安装程序&#39;并且&#39; tearDown&#39;因为在每次测试后自动调用它们都会丢失。