AttributeError: 'TextTestResult' object has no attribute 'assertIn' "AttributeError: 'TextTestResult'对象没有'assertIn'属性"
AttributeError: 'TextTestResult' object has no attribute 'assertIn' "AttributeError: 'TextTestResult'对象没有'assertIn'属性"
我正在尝试使用Selenium编写一些Python代码,我在simpleUsageUnittest.py中定义了一些测试:\n
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')
\n这部分单独运行完全正常,但是当我尝试创建一些测试套件时,testTestSuite.py出现了问题:\n
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())
\n在运行这个测试套件时,我遇到了AttributeError: \'TextTestResult\' object has no attribute \'assertIn\'的错误,由于我不太明白它的意思,所以无法修复它;)如果我在simpleUsageUnittest.py中删除assertIn行,那么它又可以正常工作,但这当然不是我想要做的!此外,Asserts in Python 2.7 not working for me example assertIn对我没有太大帮助,因为我使用的是Python 3.3.5和Selenium 2.41.0。有人能向我解释一下吗?或者指导我可以使用哪些属性来保存我的断言?;)\n完整追踪:\n
C:\Users\zzz\Python\selenium_tutorial>python testTestSuite.py Traceback (most recent call last): File "testTestSuite.py", line 14, inresult = 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'
\n解决方案\n好的,看起来在我的testTestSuite.py中,执行时TextTestRunner将simpleUsageUnittest.py中的\"self.assertIn\"行视为self == TextTestRunner而不是self == TestCase(我不知道我是否解释/理解正确,但这是我看到的方式;))。下面是修复后的testTestSuite.py:\n
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())
\n因为\'setUp\'和\'tearDown\'是在每个\'test\'之后自动调用的,所以它们被省略了。
问题原因:
出现这个错误是因为在执行testTestSuite.py时,TextTestRunner将simpleUsageUnittest.py中的"self.assertIn"行视为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())
由于在每个'test'之后自动调用'setUp'和'tearDown',因此这些方法被省略了。