Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 101 lines (68 sloc) 2.89 kb
487f7e7 @Cito Make tests in the Wiki2 tutorial a separate chapter, as for Wiki1.
Cito authored
1 ============
2 Adding Tests
3 ============
4
5 We will now add tests for the models and the views and a few functional
6 tests in the ``tests.py``. Tests ensure that an application works, and
7 that it continues to work after some changes are made in the future.
8
9 Testing the Models
10 ==================
11
12 We write a test class for the model class ``Page`` and another test class
13 for the ``initialize_sql`` function.
14
15 To do so, we'll retain the ``tutorial.tests.ViewTests`` class provided as a
16 result of the ``pyramid_routesalchemy`` project generator. We'll add two
17 test classes: one for the ``Page`` model named ``PageModelTests``, one for the
18 ``initialize_sql`` function named ``InitializeSqlTests``.
19
20 Testing the Views
21 =================
22
23 We'll modify our ``tests.py`` file, adding tests for each view function we
24 added above. As a result, we'll *delete* the ``ViewTests`` test in the file,
25 and add four other test classes: ``ViewWikiTests``, ``ViewPageTests``,
26 ``AddPageTests``, and ``EditPageTests``. These test the ``view_wiki``,
27 ``view_page``, ``add_page``, and ``edit_page`` views respectively.
28
29 Functional tests
30 ================
31
32 We test the whole application, covering security aspects that are not
33 tested in the unit tests, like logging in, logging out, checking that
34 the ``viewer`` user cannot add or edit pages, but the ``editor`` user
35 can, and so on.
36
37 Viewing the results of all our edits to ``tests.py``
38 ====================================================
39
40 Once we're done with the ``tests.py`` module, it will look a lot like the
41 below:
42
43 .. literalinclude:: src/tests/tutorial/tests.py
44 :linenos:
45 :language: python
46
47 Running the Tests
48 =================
49
50 We can run these tests by using ``setup.py test`` in the same way we did in
05a1b4a @mcdonc - The Wiki2 tutorial "Tests" chapter had two bugs: it did not tell th…
mcdonc authored
51 :ref:`running_tests`. However, first we must edit our ``setup.py`` to
52 include a dependency on WebTest, which we've used in our ``tests.py``.
53 Change the ``requires`` list in ``setup.py`` to include ``WebTest``.
54
55 .. literalinclude:: src/tests/setup.py
56 :linenos:
57 :language: python
58 :lines: 10-19
59
231f532 @mcdonc run setup.py develop
mcdonc authored
60 After we've added a dependency on WebTest in ``setup.py``, we need to rerun
61 ``setup.py develop`` to get WebTest installed into our virtualenv. Assuming
62 our shell's current working directory is the "tutorial" distribution
63 directory:
64
65 On UNIX:
66
67 .. code-block:: text
68
69 $ ../bin/python setup.py develop
70
71 On Windows:
72
73 .. code-block:: text
74
75 c:\pyramidtut\tutorial> ..\Scripts\python setup.py develop
76
77 Once that command has completed successfully, we can run the tests
78 themselves:
487f7e7 @Cito Make tests in the Wiki2 tutorial a separate chapter, as for Wiki1.
Cito authored
79
80 On UNIX:
81
82 .. code-block:: text
83
84 $ ../bin/python setup.py test -q
85
86 On Windows:
87
88 .. code-block:: text
89
90 c:\pyramidtut\tutorial> ..\Scripts\python setup.py test -q
91
92 The expected result looks something like:
93
94 .. code-block:: text
95
96 ......................
97 ----------------------------------------------------------------------
98 Ran 22 tests in 2.700s
99
100 OK
Something went wrong with that request. Please try again.