2015-06-19 74 views
0

我刚刚开始通过Test-Driven Development with Python工作,并且不明白我得到的AttributeError,因为它与书中的不同。测试山羊Python属性错误

运行Selenium测试代码:

from selenium import webdriver 
import unittest 

class new_visitor_test(unittest.TestCase): 

     def set_up(self): 
       self.browser = webdriver.Firefox() 

     def tear_down(self): 
       self.browser.quit() 

     def test_can_start_a_list_and_retrieve_it_later(self): 
       self.browser.get('http://localhost:8000') 

       self.assertIn('To-Do', self.browser.title) 
       self.fail('Finish the test!') 

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

和错误应该是:

Traceback (most recent call last): 
File "functional_tests.py", line 18, in 
test_can_start_a_list_and_retrieve_it_later 
self.assertIn('To-Do', self.browser.title) 
AssertionError: 'To-Do' not found in 'Welcome to Django' 

我得到的错误是:

Traceback (most recent call last): 
    File "functional_tests.py", line 13, in test_can_start_a_list_and_retrieve_it_later 
    self.browser.get('http://localhost:8000') 
AttributeError: 'new_visitor_test' object has no attribute 'browser' 

是什么造成这个错误?

回答

3

设置方法应该叫setUp(),推倒方法 - tearDown()

class new_visitor_test(unittest.TestCase): 
    def setUp(self): 
     self.browser = webdriver.Firefox() 

    def tearDown(self): 
     self.browser.quit() 

    def test_can_start_a_list_and_retrieve_it_later(self): 
     self.browser.get('http://localhost:8000') 

     self.assertIn('To-Do', self.browser.title) 
     self.fail('Finish the test!') 

的方法实际上是named correctly in the book

+0

哦,好的,所以必须特定于'unittest'而不是样式选择。我知道这会归结为某种新手输入错误!谢谢! – bordeltabernacle

+0

是啊,我很尴尬! – bordeltabernacle