Wing Tips: Unit Testing in Wing Pro

Jun 08, 2023


This Wing Tip introduces the Testing tool in Wing Pro, which can be used to run and debug unit tests designed for the unittest, pytest, doctest, nose, or Django testing frameworks. Wing can collect code coverage statistics, so you can see if your tests are properly exercising your code, and so that Wing can determine which tests need to be re-run after you make edits to your code.

Identifying Test Files

You can identify files to run as tests in your project either by setting Test file patterns under the Testing tab in Project Properties (accessed from the Project menu) or by adding individual test files with Add Single File and Add Current File in the Testing menu.

Running and Debugging Tests

Once you have identified test files, you should see them listed in the Testing tool and can use the Run Tests button to run them. The Testing tool will update as tests complete, indicating which tests passed and failed. This includes display of any exception encountered and output from the tests.

Failed tests may then be debugged by selecting them in the Testing tool and pressing the Debug Tests button:

Testing Tool in Wing Pro

Shown above: Running unit tests in Wing Pro, inspecting test results, and debugging one failed test.

The items in the Testing menu can be used to run or re-run tests selectively, for example only those at the caret in the editor, those that previously failed, or those that have not yet been run.

Code Coverage

To enable collection of code coverage statistics, make sure you have installed coverage (for example with pip install coverage), select Use Code Coverage in the Testing tool, and then run some or all of your unit tests. Wing annotates every line of code in editors to indicate whether that line was reached by any of the unit tests that you have run. Hover the mouse cursor over lines marked in green to see which tests reached that line:

Code Coverage Annotations

Shown above: Code coverage marks indicate which lines of code have been tested.

When code coverage is enabled, Wing will watch your edits in order to determine which previously run test results are invalidated by those edits. Any tests that reached the edited code will be marked as invalid in the Testing tool by changing the color of the test result icon to yellow:

Invalidated Unit Test Results

Shown above: A unit test result is invalidated by editing code that was reached by that test. All the tests with invalidated test results are then re-run with Run Stale Tests in the Testing menu.

An HTML code coverage report and various code coverage data export options are available in the Testing menu.

For more information on unit testing and code coverage in Wing Pro, see the Unit Testing documentation.



That's it for now! We'll be back soon with more Wing Tips for Wing Python IDE.

As always, please don't hesitate to email support@wingware.com if you run into problems or have any questions.



Share this article: