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:
– Create a a new VS solution with a Python application project (i called it “OptionGreeks”)
– Add your Python environment by right-clicking on your Python project ‘Python Environments’, in the Solution explorer:
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”.
Now, add to your solution a .py file (i called it “OptionsAnalyticsTestSuite.py”) containing the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import unittest from OptionsAnalytics import BSMerton class TestPremiums(unittest.TestCase): def test_eurcallpremium1(self): prem = BSMerton([1,100,100,0.01,0.01,360,0.2]).Premium self.assertEqual(7.8333582279074605, prem) def test_eurputpremium1(self): prem = BSMerton([-1,100,98,0.01,0.02,180,0.15]).Premium self.assertEqual(3.4205937142607468, prem) def test_eurcallcharm1(self): charm = BSMerton([1,100,102,0.05,0.02,120,0.30]).Charm self.assertEqual(-0.00039882343484384407, charm) def test_eurputcharm1(self): charm = BSMerton([-1,100,98,0.01,0.03,15,0.45]).Charm self.assertEqual(0.002342142238324095, charm) def test_eurputvanna1(self): vanna = BSMerton([-1,100,98,0.01,0.03,15,0.45]).Vanna self.assertEqual(-0.0014288903242115219, vanna) def test_eurcalltheta1(self): theta = BSMerton([1,100,98,0.01,0.03,15,0.45]).Theta self.assertEqual(-0.11377290475379419, theta) def test_eurputtheta1(self): theta = BSMerton([-1,100,98,0.01,0.03,15,0.10]).Theta self.assertEqual(-0.017791872439820017, theta) def test_eurcallvega1(self): vega = BSMerton([1,100,98,0.01,0.03,15,0.45]).Vega self.assertEqual(0.078129041928971896, vega) def test_eurcallgamma1(self): gamma = BSMerton([1,100,98,0.01,0.03,15,0.45]).Gamma self.assertEqual(0.042247556006036656, gamma) def test_eurcallrho1(self): rho = BSMerton([1,100,105,0.05,0.03,60,0.45]).Rho self.assertEqual(0.062774269424770157, rho) def test_eurputrho1(self): rho = BSMerton([-1,100,105,0.05,0.03,60,0.45]).Rho self.assertEqual(-0.10841563178532679, rho) def test_eurcallphi1(self): phi = BSMerton([1,100,105,0.05,0.03,60,0.45]).Phi self.assertEqual(-0.071508393409630486, phi) def test_eurputphi1(self): phi = BSMerton([-1,100,105,0.05,0.03,60,0.45]).Phi self.assertEqual(0.092066505176620975, phi) if __name__ == '__main__': unittest.main() |
The Python project should look like:
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:
Leave a Reply