Skip to content
Myr

testing.rst

This file

Path

docs/tutorial/testing.rst

96 lines · 2.9 KiB

Last changed by

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

History of this file

Take it

The walk stopped after 200 commits: lines older than d45445e point at it.

Merge pull request #1743 from homoSapiensSapiens/use_pytest_raisesMarkus Unterwaditzerbefore adoptiond45445e

lines 1–10why, one step back
1.. _tutorial-testing:
23Bonus: Testing the Application4==============================56Now that you have finished the application and everything works as7expected, it's probably not a bad idea to add automated tests to simplify8modifications in the future. The application above is used as a basic9example of how to perform unit testing in the :ref:`testing` section of the10documentation. Go there to see how easy it is to test Flask applications.

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

line 11why, one step back
11

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 12–13why, one step back
12Adding tests to flaskr
13----------------------

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

line 14why, one step back
14

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

line 15why, one step back
15Assuming you have seen the :ref:`testing` section and have either written

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

lines 16–30why, one step back
16your own tests for ``flaskr`` or have followed along with the examples
17provided, you might be wondering about ways to organize the project.1819One possible and recommended project structure is::2021 flaskr/22 flaskr/23 __init__.py24 static/25 templates/26 tests/27 test_flaskr.py28 setup.py29 MANIFEST.in30

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 31–32why, one step back
31For now go ahead a create the :file:`tests/` directory as well as the
32:file:`test_flaskr.py` file.

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

line 33why, one step back
33

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 34–35why, one step back
34Running the tests
35-----------------

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

line 36why, one step back
36

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

line 37why, one step back
37At this point you can run the tests. Here ``pytest`` will be used.

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

line 38why, one step back
38

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 39–41why, one step back
39.. note:: Make sure that ``pytest`` is installed in the same virtualenv
40 as flaskr. Otherwise ``pytest`` test will not be able to import the 41 required components to test the application::

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

line 42why, one step back
42

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 43–44why, one step back
43 pip install -e .
44 pip install pytest

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

line 45why, one step back
45

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 46–47why, one step back
46Run and watch the tests pass, within the top-level :file:`flaskr/`
47directory as::

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

line 48why, one step back
48

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

lines 49–59why, one step back
49 py.test
5051Testing + setuptools52--------------------5354One way to handle testing is to integrate it with ``setuptools``. Here55that requires adding a couple of lines to the :file:`setup.py` file and56creating a new file :file:`setup.cfg`. One benefit of running the tests 57this way is that you do not have to install ``pytest``. Go ahead and 58update the :file:`setup.py` file to contain::59

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

lines 60–75why, one step back
60 from setuptools import setup
6162 setup(63 name='flaskr',64 packages=['flaskr'],65 include_package_data=True,66 install_requires=[67 'flask',68 ],69 setup_requires=[70 'pytest-runner',71 ],72 tests_require=[73 'pytest',74 ],75 )

Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb

line 76why, one step back
76

Merge branch '0.11-maintenance'David Lordbefore adoption633b13e

lines 77–96why, one step back
77Now create :file:`setup.cfg` in the project root (alongside
78:file:`setup.py`)::7980 [aliases]81 test=pytest8283Now you can run::8485 python setup.py test8687This calls on the alias created in :file:`setup.cfg` which in turn runs88``pytest`` via ``pytest-runner``, as the :file:`setup.py` script has89been called. (Recall the `setup_requires` argument in :file:`setup.py`)90Following the standard rules of test-discovery your tests will be91found, run, and hopefully pass.9293This is one possible way to run and manage testing. Here ``pytest`` is94used, but there are other options such as ``nose``. Integrating testing95with ``setuptools`` is convenient because it is not necessary to actually96download ``pytest`` or any other testing framework one might use.