testing.rst
This file
Path
docs/tutorial/testing.rst
88 lines · 2.6 KiB
Last changed by
Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
History of this fileTake it
The walk stopped after 200 commits: lines older than ecb8f81 point at it.
Merge pull request #1389 from untitaker/always-memory-testMarkus Unterwaditzerbefore adoptionecb8f81
lines 1–10why, one step back1.. _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
lines 11–88why, one step back1112Adding Tests to flaskr13======================1415Assuming you have seen the testing section above and have either written16your own tests for ``flaskr`` or have followed along with the examples17provided, 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 context.py28 test_flaskr.py29 setup.py30 MANIFEST.in3132For now go ahead a create the :file:`tests/` directory as well as the33:file:`context.py` and :file:`test_flaskr.py` files, if you haven't34already. The context file is used as an import helper. The contents35of that file are::3637 import sys, os3839 basedir = os.path.dirname(os.path.abspath(__file__))40 sys.path.insert(0, basedir + '/../')4142 from flaskr import flaskr4344Testing + Setuptools45====================4647One way to handle testing is to integrate it with ``setuptools``. All it48requires is adding a couple of lines to the :file:`setup.py` file and49creating a new file :file:`setup.cfg`. Go ahead and update the50:file:`setup.py` to contain::5152 from setuptools import setup5354 setup(55 name='flaskr',56 packages=['flaskr'],57 include_package_data=True,58 install_requires=[59 'flask',60 ],61 )62 setup_requires=[63 'pytest-runner',64 ],65 tests_require=[66 'pytest',67 ],68 )69Now create :file:`setup.cfg` in the project root (alongside70:file:`setup.py`)::7172 [aliases]73 test=pytest7475Now you can run::7677 python setup.py test7879This calls on the alias created in :file:`setup.cfg` which in turn runs80``pytest`` via ``pytest-runner``, as the :file:`setup.py` script has81been called. (Recall the `setup_requires` argument in :file:`setup.py`)82Following the standard rules of test-discovery your tests will be83found, run, and hopefully pass.8485This is one possible way to run and manage testing. Here ``pytest`` is86used, but there are other options such as ``nose``. Integrating testing87with ``setuptools`` is convenient because it is not necessary to actually88download ``pytest`` or any other testing framework one might use.