Steve Piercy
2017-06-26 19d341b5be789e97000d3dcbd33de75d8b061829
commit | author | age
b731b5 1 .. _qtut_functional_testing:
PE 2
b1b922 3 ===================================
PE 4 06: Functional Testing with WebTest
5 ===================================
6
7 Write end-to-end full-stack testing using ``webtest``.
8
ccf397 9
b1b922 10 Background
PE 11 ==========
12
ccf397 13 Unit tests are a common and popular approach to test-driven development (TDD).
SP 14 In web applications, though, the templating and entire apparatus of a web site
15 are important parts of the delivered quality. We'd like a way to test these.
b1b922 16
19d341 17 `WebTest <https://docs.pylonsproject.org/projects/webtest/en/latest/>`_ is a
ccf397 18 Python package that does functional testing. With WebTest you can write tests
SP 19 which simulate a full HTTP request against a WSGI application, then test the
20 information in the response. For speed purposes, WebTest skips the
21 setup/teardown of an actual HTTP server, providing tests that run fast enough
22 to be part of TDD.
23
b1b922 24
PE 25 Objectives
26 ==========
27
ccf397 28 - Write a test which checks the contents of the returned HTML.
SP 29
b1b922 30
PE 31 Steps
32 =====
33
ccf397 34 #. First we copy the results of the previous step, as well as install the
SP 35    ``webtest`` package:
b1b922 36
PE 37    .. code-block:: bash
38
187104 39     $ cd ..; cp -r unit_testing functional_testing; cd functional_testing
9459d8 40     $ $VENV/bin/pip install -e .
SP 41     $ $VENV/bin/pip install webtest
b1b922 42
c92f49 43 #. Let's extend ``functional_testing/tutorial/tests.py`` to include a
b1b922 44    functional test:
PE 45
46    .. literalinclude:: functional_testing/tutorial/tests.py
47     :linenos:
48
ccf397 49    Be sure this file is not executable, or ``pytest`` may not include your
SP 50    tests.
c92f49 51    
b1b922 52 #. Now run the tests:
PE 53
54    .. code-block:: bash
55
ccf397 56     $ $VENV/bin/py.test tutorial/tests.py -q
SP 57     ..
58     2 passed in 0.25 seconds
b1b922 59
PE 60
61 Analysis
62 ========
63
ccf397 64 We now have the end-to-end testing we were looking for. WebTest lets us simply
SP 65 extend our existing ``pytest``-based test approach with functional tests that
66 are reported in the same output. These new tests not only cover our templating,
67 but they didn't dramatically increase the execution time of our tests.
b1b922 68
ccf397 69
SP 70 Extra credit
b1b922 71 ============
PE 72
c92f49 73 #. Why do our functional tests use ``b''``?