2

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.

1 Answer 1

4

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()

    ...
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that makes the code fail as expected. Could you explain to me why that makes it work? Sorry for my ignorance.
@w0nk, The names of the methods should match (Python does distinguish identifier cases). And the TestCase expect setUp, tearDown, not setup, teardown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.