Documentation in progress Edit me on GitHub

06: Functional Testing with WebTest

Write end-to-end full-stack testing using webtest.

Background

Unit tests are a common and popular approach to test-driven development (TDD.) In web applications, though, the templating and entire apparatus of a web site are important parts of the delivered quality. We'd like a way to test these.

WebTest is a Python package that does functional testing. With WebTest you can write tests which simulate a full HTTP request against a WSGI application, then test the information in the response. For speed purposes, WebTest skips the setup/teardown of an actual HTTP server, providing tests that run fast enough to be part of TDD.

Objectives

  • Write a test which checks the contents of the returned HTML

Steps

  1. First we copy the results of the previous step, as well as install the webtest package:

    $ cd ..; cp -r unit_testing functional_testing; cd functional_testing
    $ $VENV/bin/python setup.py develop
    $ $VENV/bin/easy_install webtest
    
  2. Let's extend unit_testing/tutorial/tests.py to include a functional test:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    import unittest
    
    from pyramid import testing
    
    
    class TutorialViewTests(unittest.TestCase):
        def setUp(self):
            self.config = testing.setUp()
    
        def tearDown(self):
            testing.tearDown()
    
        def test_hello_world(self):
            from tutorial import hello_world
    
            request = testing.DummyRequest()
            response = hello_world(request)
            self.assertEqual(response.status_code, 200)
    
    
    class TutorialFunctionalTests(unittest.TestCase):
        def setUp(self):
            from tutorial import main
            app = main({})
            from webtest import TestApp
    
            self.testapp = TestApp(app)
    
        def test_hello_world(self):
            res = self.testapp.get('/', status=200)
            self.assertIn(b'<h1>Hello World!</h1>', res.body)
    
  3. Now run the tests:

    $ $VENV/bin/nosetests tutorial
    .
    ----------------------------------------------------------------------
    Ran 2 tests in 0.141s
    
    OK
    

Analysis

We now have the end-to-end testing we were looking for. WebTest lets us simply extend our existing nose-based test approach with functional tests that are reported in the same output. These new tests not only cover our templating, but they didn't dramatically increase the execution time of our tests.

Extra Credit

  1. Why do our functional tests use b''?

Table Of Contents

Previous topic

05: Unit Tests and nose

Next topic

07: Basic Web Handling With Views