Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is my code;

from selenium import webdriver
import unittest

class NewVisitorTest(unittest.TestCase):

    def setup(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(5)

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

    def test_can_start_list_and_retrieve_later(self):
    # checkout the homepage
        self.browser.get('http://127.0.0.1:8000')

    # check title and header
        self.assertIn('To-Do', self.browser.title)
        self.fail('Finish the test')

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

which results in the following error:

$ python3 functional_tests.py 
E
======================================================================
ERROR: test_can_start_list_and_retrieve_later         (__main__.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "functional_tests.py", line 15, in     test_can_start_list_and_retrieve_later
self.browser.get('http://127.0.0.1:8000')
AttributeError: 'NewVisitorTest' object has no attribute 'browser'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

the code is taken from TDD with python

My code matches with what has beeen written in the tutorial but should produce an assertIn error as the page has yet to be set up. Any help would be appreciated.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

There are typos: setup should be setUp. And teardown should be tearDown.

class NewVisitorTest(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(5)

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

    ...
share|improve this answer
    
Thank you, that makes the code fail as expected. Could you explain to me why that makes it work? Sorry for my ignorance. –  w0nk Jan 17 at 9:03
    
@w0nk, The names of the methods should match (Python does distinguish identifier cases). And the TestCase expect setUp, tearDown, not setup, teardown. –  falsetru Jan 17 at 9:05
    
@w0nk, Welcome to Stack Overflow! If this helped you, you can tell the community so by accepting the answer. –  falsetru Jan 17 at 9:06
    
done and thanks –  w0nk Jan 17 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.