Tutorial – Python unit test with Eclipse (1)

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

1
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

2

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.

3

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")

4

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:

5

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!

Advertisements

One thought on “Tutorial – Python unit test with Eclipse (1)

  1. very cool tutorial. Makes me wanna install Windows to try this out!
    One thing about your blog setup,
    there should be code highlighting and formatting from within WordPress right?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s