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 |
|
8998f6
|
34 |
#. First we copy the results of the previous step. |
b1b922
|
35 |
|
8998f6
|
36 |
.. code-block:: bash |
b1b922
|
37 |
|
8998f6
|
38 |
cd ..; cp -r unit_testing functional_testing; cd functional_testing |
b1b922
|
39 |
|
8998f6
|
40 |
#. Add ``webtest`` to our project's dependencies in ``setup.py`` as a :term:`Setuptools` "extra": |
b1b922
|
41 |
|
8998f6
|
42 |
.. literalinclude:: functional_testing/setup.py |
SP |
43 |
:language: python |
|
44 |
:linenos: |
753580
|
45 |
:emphasize-lines: 16 |
b1b922
|
46 |
|
8998f6
|
47 |
#. Install our project and its newly added dependency. |
23fbca
|
48 |
Note that we use the extra specifier ``[dev]`` to install testing requirements and surround it with double quote marks. |
8998f6
|
49 |
|
SP |
50 |
.. code-block:: bash |
|
51 |
|
23fbca
|
52 |
$VENV/bin/pip install -e ".[dev]" |
8998f6
|
53 |
|
SP |
54 |
#. Let's extend ``functional_testing/tutorial/tests.py`` to include a functional test: |
|
55 |
|
|
56 |
.. literalinclude:: functional_testing/tutorial/tests.py |
|
57 |
:linenos: |
|
58 |
|
|
59 |
Be sure this file is not executable, or ``pytest`` may not include your tests. |
c92f49
|
60 |
|
8998f6
|
61 |
#. Now run the tests: |
b1b922
|
62 |
|
8998f6
|
63 |
.. code-block:: bash |
b1b922
|
64 |
|
8998f6
|
65 |
$VENV/bin/pytest tutorial/tests.py -q |
SP |
66 |
.. |
|
67 |
2 passed in 0.25 seconds |
b1b922
|
68 |
|
PE |
69 |
|
|
70 |
Analysis |
|
71 |
======== |
|
72 |
|
ccf397
|
73 |
We now have the end-to-end testing we were looking for. WebTest lets us simply |
SP |
74 |
extend our existing ``pytest``-based test approach with functional tests that |
|
75 |
are reported in the same output. These new tests not only cover our templating, |
|
76 |
but they didn't dramatically increase the execution time of our tests. |
b1b922
|
77 |
|
ccf397
|
78 |
|
SP |
79 |
Extra credit |
b1b922
|
80 |
============ |
PE |
81 |
|
c92f49
|
82 |
#. Why do our functional tests use ``b''``? |