Noob question on running Python file Instantiated Class Methods: I want to run the method test_method() in the class TestClass() to verify algorithm() works. How do I run this code from the python command line?
File Name: algo.py
import unittest
import numpy as np
import pandas as pd
from datetime import datetime
def algorithm(a,b):
c = a+b
return c
class TestClass(unittest.TestCase):
def test_method(self):
a = np.array(1.1)
b = np.array(2.2)
algo_return = algorithm(a,b)
self.assertAlmostEqual(algo_return[0], 3.3)
I have attempted,
import algo
test_method = algo
TestClass = algo.TestClass()
But the third line yielded the result:
Traceback (most recent call last):
File "/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 191, in __init__
(self.__class__, methodName))
ValueError: no such test method in <class 'algo.TestClass'>: runTest
I am using python2.7, wings101 IDE.
I'm looking for an answer like (But this doesn't work):
import algo
TestClass.test_method()
Add the following code to your file:
if __name__ == '__main__':
unittest.main()
Then you'll be able to run your file simply as "python algo.py", and that will run your test.
See here for more information on using the Python unittest framework.