In this article, I will walk through how to set up and do a python unit test with Eclipse.
Prerequisite: Pydev has been installed in Eclipse. If not, please open up the Eclipse and go to: Help -> Eclipse MarketPlace and search for ‘PyDev’ and install it as below
Now, we are ready to create a python unit test. To start with, let’s create a new PyDev Project for holding the project source and the unit tests.
1) We go to: File -> New, in the New window, choose PyDev Project as below
and give the project a name, such as TestPython
2) In the project TestPython, create a new python module on top of it in order to have all our unit tests placed inside this model.
and give the package as test and name as testCalculator
3) We now have the generated package: test and two files created. In theory, we can put as many unit tests in this package. In the testCalculator.py, put the following code there
import unittest
class TestCalc(unittest.TestCase):
def testAdd(self):
print("it is a test")
result = True
self.assertEqual(result, True, "Ohno")
Basically, in the above code, we just created a test class: TestCalc which extends the unittest.TestCase as the base class and the TestCalc class will have all the testing API available such as self.assertEqual..etc. For more information of the API of unittest, please feel free to refer: https://docs.python.org/3/library/unittest.html
4) The last step would be to just right click the testCalculator.py in the Package Explorer and choose Run As -> Python unit-test. If the setup is correct, we should see something like this:
And yes, congrats! you have just created the basic python unit test!
Stay tuned and more to come (such as running it in the command line instead) later!