测试Goat中的Python AttributeError

时间:2015-06-19 15:29:58

标签: python unit-testing selenium

我刚开始使用Test-Driven Development with 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'

造成此错误的原因是什么?

1 个答案:

答案 0 :(得分: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