Python Unit Tests

This article covers briefly how you can run Python Unit Tests against a suite of analytics in Visual Studio 2010 with Python Tools for Visual Studio (PTVS). Visual Studio 2013 Express edition is much more adapted to Python unit testing. Here we explain how you can circumvent this limitation using a main() method to launch this series of tests.

Prerequisites

– Install Visual Studio 2010 (full version), or VS Pro (Community Edition, which is free) .  You can also install PTVS 2.1 into VS 2013 Express for Web and VS 2013 Express for Desktop editions.

– Install PTVS adpated to your VS version (2013,2013).

– Once you have installed a Python distribution like  Anaconda, configure PTVS options as following in VS Tools, Options, Python Tools:

Unit tests Python

Unit tests Python

– Create a a new VS solution with a Python application project (i called it “OptionGreeks”)

Python unit test

PTVS New project

– Add your Python environment by right-clicking on your Python project ‘Python Environments’, in the Solution explorer:

Python Unit Test

PTVS add environment

Analytics library to be tested

The class that we are going to test here is already published in a previous article dedicated to options greeks computation.

Add this .py (OptionsAnalytics.py) to your project.

Unit tests

Depending on your python distribution, you may have to download the package unittest: right-click on your Python environment and select “Install Python Package”.

PTVS Add unittestpackage

 

Now, add to your solution a .py file (i called it “OptionsAnalyticsTestSuite.py”) containing the following code:

The Python project should look like:

Python Unit tests

If you set OptionsAnalyticsTestSuite.py to be the startup project, just run it (F5) and you should see in the console window that all tests passed:

Python Unit Tests

Test Results

Facebooktwittergoogle_plusredditlinkedinmail