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 fileTake it
The walk stopped after 200 commits: lines older than c1c205b point at it.
Merge pull request #1661 from lord63-forks/clarify-versionDavid Lordbefore adoptionc1c205b
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
line 11why, one step back11Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 12–13why, one step back12Adding tests to flaskr13----------------------Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 14why, one step back14Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
line 15why, one step back15Assuming you have seen the :ref:`testing` section and have either writtenMerge branch '0.11-maintenance'David Lordbefore adoption633b13e
lines 16–30why, one step back16your 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 test_flaskr.py28 setup.py29 MANIFEST.in30Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 31–32why, one step back31For 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 back33Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 34–35why, one step back34Running the tests35-----------------Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 36why, one step back36Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
line 37why, one step back37At this point you can run the tests. Here ``pytest`` will be used. Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 38why, one step back38Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 39–41why, one step back39.. 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 back42Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 43–44why, one step back43 pip install -e .44 pip install pytest Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 45why, one step back45Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 46–47why, one step back46Run 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 back48Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 49–59why, one step back49 py.test5051Testing + 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::59Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
lines 60–75why, one step back60 from setuptools import setup6162 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 back76Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
lines 77–96why, one step back77Now create :file:`setup.cfg` in the project root (alongside78: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.