O'Reilly logo

Python Testing Beginner's Guide by Daniel Arbuckle

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - using Twill's browser object

Here we see how to access the browser object directly, and use it to interact with the web.

  1. Place the following code into a Python test module:
    from unittest import TestCase
    import twill
    class test_twill_browser(TestCase):
    def test_slashdot(self):
    browser = twill.get_browser()
    browser.go('http://slashdot.org/')
    self.assertTrue(browser.get_code() in (200, 201))
    html = browser.get_html()
    self.assertTrue(html.count('slashdot') > 150)
    link = browser.find_link('Science')
    browser.follow_link(link)
    form = browser.get_form(2)
    form.set_value('aardvark', name = 'fhfilter')
    browser.clicked(form, None)
    browser.submit()
    self.assertEqual(browser.get_code(), 200)
    
  2. Run the test module using nosetests. If Slashdot hasn't ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required