Week 9 Practical – PyUnit

For this week, I’m going to work on a framework of unit testing for Python: PyUnit. It is also going to be used with PyCharm an IDE for python available at JetBrains. This blog includes a quick tutorial on how to make unit testing on python, as well as brief comments on a video made for this assignment by Richard Wells. This video is available on LinkedIn Learning (previously Lynda). Also, here is an article by Kent Beck that. proposes a testing framework on Smalltalk. There are several comments from classmates in there, comments are available through hypothes.is.

Screen Shot 2019-03-17 at 11.21.46 AM.png
Comment on Kent Beck article on Smalltalk testing framework

Note that this article is shown on Wayback Machine, it is a very cool platform where you can look up web sites at different points in time. For example, here is Blockbuster on December 24, 1996. You can really tell how websites have changed in around 20 years.

Screen Shot 2019-03-17 at 11.36.28 AM.png

First we need to make a project in PyCharm, to do so open PyCharm and if there’s no previous project you click on new project. You do not need any special configurations for testing environments. For this example, I am going to make a python class called Calculator.py, it contains the four basic operations: sum, subtraction, multiplication and division. The math import is not necessary as the operations implemented do not require it, I’ll leave it in case you need some other operations such as trigonometry operators or sqrt.

import math
class Calculator:
def add(self, a , b):
    return (a + b)

def substract(self, a, b):
    return (a - b)

def multiply(self, a, b):
    return (a * b)

def divide(self, a , b):
    return(a / b)

Then you can right click on the file and Go To then Test. This will open a dialog to choose which methods you want to generate tests. Select the methods you want to test, and once you click OK, a file like the one below will be generated.

class TestCalculator(TestCase):
    def test_add(self):
        self.fail()

    def test_substract(self):
        self.fail()

    def test_multiply(self):
        self.fail()

    def test_divide(self):
        self.fail()

self.fail() is stating that the test is going to fail. So we need to describe the tests so that we can really check if the code is working as expected. First we need to import the class we just did, we import it like this: from Calculator import Calculator. Now we develop the test cases, for every method generated, we write a test. For example, for the addition method, the test case will look like this:

    def test_add(self):
        calc = Calculator()
        self.assertEqual(2, calc.add(1, 1))

If the test was successful, then it will run smoothly, but if it is not, we will get an error with the condition that failed the test as shown below.

FAILED (failures=1)
1.0 != 11

Expected :11
Actual :1.0

On the other hand, here is a test that was successfully ran.

Ran 4 tests in 0.001s
OK

If you want to dig deeper on PyUnit and PyCharm, I recommend the video in the description above.

Leave a comment