<type>documentation</type>
<domain>intro</domain>
<id>tutorial-testing</id>
<title>Tutorial: Unit Testing</title>
<date>$Date: 2013-06-27 19:22:37 -0400 (Thu, 27 Jun 2013) $</date>
<product>pro+</product>

Wing Pro's `Testing <script:show_panel#panel_type="testing">`__ tool makes it easy to run
and debug units tests written for the ``unittest``, ``doctest``, ``pytest``, ``nose``, and
``Django`` unit testing frameworks.

Let's try this out now. First, open up ``Project Properties`` and under the ``Testing``
tab insert a ``Test File Pattern`` that is set to ``Glob / Wildcard`` and ``test_*.py``.
This tells Wing which of your project files are unit test files. Press ``OK`` or ``Apply``
and bring up the ``Testing`` tool from the ``Tools`` menu. This should now contain an
entry for the file ``test_example1.py``:

.. image:: testing.png
  :width: 431
  :height: 140

Next comment out the line that reads ``PromptToContinue`` in ``example1.py`` so that the
module can be loaded by the tests without prompting.

Then press ``Run Tests`` in the ``Testing`` tool. You should see two of the three tests
pass, and one will fail:

.. image:: testing-results.png
  :width: 432
  :height: 260

You can expand the tree to see details of the failed tests, including any output printed
by the test and the exception that occurred. Double-clicking on the test results and
exception will take you to the relevant code.

Note that you can also run tests from the editor by clicking on the test you want to run
and selecting ``Run Tests at Cursor`` from the ``Testing`` menu.

**Debugging Tests**

Now run the failed ``testFailure`` in the debugger by clicking on it in the ``Testing``
tool and pressing ``Debug Tests`` at the top of the tool. Wing should stop at the
exception and you can use the debugger on the test as you would for any other Python code.

**Environment**

When unit tests are run in the ``Testing`` tool, by default they run in the same
environment that is used for debugging and executing code. This can be changed with
``Environment`` under the ``Testing`` tab of `Project Properties
<ref:proj/project-properties>`__ or in the `File Properties
<ref:proj/file-properties>`__ for the unit test file.

See `Unit Testing <ref:testing/index>`__ for details.

