Skip to content
Myr

testing.rst

This file

Path

docs/tutorial/testing.rst

96 lines · 2.9 KiB

Last changed by

resolve merge conflictsKenneth Reitzbefore adoption31174fe

History of this file

Take it

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.1112Adding tests to flaskr13----------------------1415Assuming you have seen the :ref:`testing` section 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            test_flaskr.py28        setup.py29        MANIFEST.in3031For now go ahead a create the :file:`tests/` directory as well as the 32:file:`test_flaskr.py` file.3334Running the tests35-----------------3637At this point you can run the tests. Here ``pytest`` will be used. 3839.. 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::4243        pip install -e .44        pip install pytest 4546Run and watch the tests pass, within the top-level :file:`flaskr/` 47directory as::4849    pytest5051Testing + 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::5960    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    )7677Now 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.