Debian Codegraph @

docs/source/testing.rst

raw
1 .. _testing:
2
3 Testing Pecan Applications
4 ==========================
5 Tests can live anywhere in your Pecan project as long as the test runner can
6 discover them. Traditionally, they exist in a package named ``myapp.tests``.
7
8 The suggested mechanism for unit and integration testing of a Pecan application
9 is the :mod:`unittest` module.
10
11 Test Discovery and Other Tools
12 ------------------------------
13
14 Tests for a Pecan project can be invoked as simply as ``python setup.py test``,
15 though it's possible to run your tests with different discovery and automation
16 tools. In particular, Pecan projects are known to work well with
17 `nose <http://pypi.python.org/pypi/nose/1.1.2>`_, `pytest
18 <http://pytest.org>`_,
19 and `tox <http://pypi.python.org/pypi/tox>`_.
20
21 Writing Functional Tests with WebTest
22 -------------------------------------
23 A **unit test** typically relies on "mock" or "fake" objects to give the code
24 under test enough context to run. In this way, only an individual unit of
25 source code is tested.
26
27 A healthy suite of tests combines **unit tests** with **functional tests**. In
28 the context of a Pecan application, functional tests can be written with the
29 help of the :mod:`webtest` library. In this way, it is possible to write tests
30 that verify the behavior of an HTTP request life cycle from the controller
31 routing down to the HTTP response. The following is an example that is
32 similar to the one included with Pecan's quickstart project.
33
34 ::
35
36 # myapp/myapp/tests/__init__.py
37
38 import os
39 from unittest import TestCase
40 from pecan import set_config
41 from pecan.testing import load_test_app
42
43 class FunctionalTest(TestCase):
44 """
45 Used for functional tests where you need to test your
46 literal application and its integration with the framework.
47 """
48
49 def setUp(self):
50 self.app = load_test_app(os.path.join(
51 os.path.dirname(__file__),
52 'config.py'
53 ))
54
55 def tearDown(self):
56 set_config({}, overwrite=True)
57
58 The testing utility included with Pecan, :func:`pecan.testing.load_test_app`, can
59 be passed a file path representing a Pecan configuration file, and will return
60 an instance of the application, wrapped in a :class:`~webtest.app.TestApp`
61 environment.
62
63 From here, it's possible to extend the :class:`FunctionalTest` base class and write
64 tests that issue simulated HTTP requests.
65
66 ::
67
68 class TestIndex(FunctionalTest):
69
70 def test_index(self):
71 resp = self.app.get('/')
72 assert resp.status_int == 200
73 assert 'Hello, World' in resp.body
74
75 .. seealso::
76
77 See the :mod:`webtest` documentation
78 for further information about the methods available to a
79 :class:`~webtest.app.TestApp` instance.
80
81 Special Testing Variables
82 -------------------------
83
84 Sometimes it's not enough to make assertions about the response body of certain
85 requests. To aid in inspection, Pecan applications provide a special set of
86 "testing variables" to any :class:`~webtest.response.TestResponse` object.
87
88 Let's suppose that your Pecan applicaton had some controller which took a
89 ``name`` as an optional argument in the URL.
90
91 ::
92
93 # myapp/myapp/controllers/root.py
94 from pecan import expose
95
96 class RootController(object):
97
98 @expose('index.html')
99 def index(self, name='Joe'):
100 """A request to / will access this controller"""
101 return dict(name=name)
102
103 and rendered that name in it's template (and thus, the response body).
104
105 ::
106
107 # myapp/myapp/templates/index.html
108 Hello, ${name}!
109
110 A functional test for this controller might look something like
111
112 ::
113
114 class TestIndex(FunctionalTest):
115
116 def test_index(self):
117 resp = self.app.get('/')
118 assert resp.status_int == 200
119 assert 'Hello, Joe!' in resp.body
120
121 In addition to :attr:`webtest.TestResponse.body`, Pecan also provides
122 :attr:`webtest.TestResponse.namespace`, which represents the template namespace
123 returned from the controller, and :attr:`webtest.TestResponse.template_name`, which
124 contains the name of the template used.
125
126 ::
127
128 class TestIndex(FunctionalTest):
129
130 def test_index(self):
131 resp = self.app.get('/')
132 assert resp.status_int == 200
133 assert resp.namespace == {'name': 'Joe'}
134 assert resp.template_name == 'index.html'
135
136 In this way, it's possible to test the return value and rendered template of
137 individual controllers.