Steve Piercy
2017-06-26 19d341b5be789e97000d3dcbd33de75d8b061829
commit | author | age
640d77 1 .. _wiki2_adding_tests:
SP 2
487f7e 3 ============
CZ 4 Adding Tests
5 ============
6
fe4465 7 We will now add tests for the models and views as well as a few functional
SP 8 tests in a new ``tests`` subpackage.  Tests ensure that an application works,
9 and that it continues to work when changes are made in the future.
10
b29848 11 The file ``tests.py`` was generated as part of the ``alchemy`` cookiecutter, but it
fe4465 12 is a common practice to put tests into a ``tests`` subpackage, especially as
SP 13 projects grow in size and complexity.  Each module in the test subpackage
14 should contain tests for its corresponding module in our application.  Each
15 corresponding pair of modules should have the same names, except the test
f10fb2 16 module should have the prefix ``test_``.
fe4465 17
606f75 18 Start by deleting ``tests.py``, then create a new directory to contain our new
SP 19 tests as well as a new empty file ``tests/__init__.py``.
fe4465 20
1c1080 21 .. warning::
MM 22
606f75 23    It is very important when refactoring a Python module into a package to be
SP 24    sure to delete the cache files (``.pyc`` files or ``__pycache__`` folders)
25    sitting around! Python will prioritize the cache files before traversing
26    into folders, using the old code, and you will wonder why none of your
27    changes are working!
1c1080 28
487f7e 29
6901d7 30 Test the views
SP 31 ==============
487f7e 32
606f75 33 We'll create a new ``tests/test_views.py`` file, adding a ``BaseTest`` class
SP 34 used as the base for other test classes. Next we'll add tests for each view
35 function we previously added to our application. We'll add four test classes:
36 ``ViewWikiTests``, ``ViewPageTests``, ``AddPageTests``, and ``EditPageTests``.
37 These test the ``view_wiki``, ``view_page``, ``add_page``, and ``edit_page``
38 views.
fe4465 39
487f7e 40
CZ 41 Functional tests
42 ================
43
606f75 44 We'll test the whole application, covering security aspects that are not tested
SP 45 in the unit tests, like logging in, logging out, checking that the ``basic``
46 user cannot edit pages that it didn't create but the ``editor`` user can, and
47 so on.
487f7e 48
CZ 49
fe4465 50 View the results of all our edits to ``tests`` subpackage
SP 51 =========================================================
52
47e51d 53 Create ``tutorial/tests/test_views.py`` such that it appears as follows:
fe4465 54
SP 55 .. literalinclude:: src/tests/tutorial/tests/test_views.py
56    :linenos:
57    :language: python
58
47e51d 59 Create ``tutorial/tests/test_functional.py`` such that it appears as follows:
fe4465 60
f10fb2 61 .. literalinclude:: src/tests/tutorial/tests/test_functional.py
47e51d 62    :linenos:
SP 63    :language: python
64
65 Create ``tutorial/tests/test_initdb.py`` such that it appears as follows:
66
67 .. literalinclude:: src/tests/tutorial/tests/test_initdb.py
68    :linenos:
69    :language: python
70
71 Create ``tutorial/tests/test_security.py`` such that it appears as follows:
72
73 .. literalinclude:: src/tests/tutorial/tests/test_security.py
74    :linenos:
75    :language: python
76
77 Create ``tutorial/tests/test_user_model.py`` such that it appears as follows:
78
79 .. literalinclude:: src/tests/tutorial/tests/test_user_model.py
fe4465 80    :linenos:
SP 81    :language: python
82
487f7e 83
66fabb 84 .. note::
MM 85
606f75 86    We're utilizing the excellent WebTest_ package to do functional testing of
SP 87    the application. This is defined in the ``tests_require`` section of our
88    ``setup.py``. Any other dependencies needed only for testing purposes can be
89    added there and will be installed automatically when running
66fabb 90    ``setup.py test``.
MM 91
92
6901d7 93 Running the tests
487f7e 94 =================
CZ 95
47e51d 96 We can run these tests similarly to how we did in :ref:`running_tests`, but first delete the SQLite database ``tutorial.sqlite``. If you do not delete the database, then you will see an integrity error when running the tests.
487f7e 97
CZ 98 On UNIX:
99
fe4465 100 .. code-block:: bash
487f7e 101
47e51d 102    $ rm tutorial.sqlite
e35dcb 103    $ $VENV/bin/py.test -q
487f7e 104
CZ 105 On Windows:
106
a651b3 107 .. code-block:: doscon
487f7e 108
47e51d 109    c:\tutorial> del tutorial.sqlite
b29848 110    c:\tutorial> %VENV%\Scripts\py.test -q
487f7e 111
6901d7 112 The expected result should look like the following:
487f7e 113
CZ 114 .. code-block:: text
115
47e51d 116    ................................
SP 117    32 passed in 9.90 seconds
de3062 118
19d341 119 .. _webtest: https://docs.pylonsproject.org/projects/webtest/en/latest/