You have a piece of code and you want/need to test it. Let’s get on with it! We will use a previous post as the code to test (you can find it here: Design Patterns: Factory Method) and will make use of Python’s unit testing framework (unittest) to build our tests.

Let’s not get ahead of ourselves. First, a common testing practice: to test their code many developers write some function that makes use of it and check if it works properly. Let’s check a possible example for our code.

def test():
    name_factory = NameFactory()

    name1 = name_factory.get_name("Castro,Ricardo")
    name2 = name_factory.get_name("Pedro Teixeira")

    print name1.get_first(), name1.get_last()
    print name2.get_first(), name2.get_last()

While this might be fine for our small example, that’s not what we really need to properly test our code. Let’s see a simple implementation for some tests and analyse it.

import unittest
from namer import NameFactory

class TestNameFactory(unittest.TestCase):
    def setUp(self):
        self.name_factory = NameFactory()

    def test_firstfirst(self):
        name = self.name_factory.get_name("Pedro Teixeira")

        self.assertEqual(name.get_first(), "Pedro")
        self.assertEqual(name.get_last(), "Teixeira")

    def test_lastfirst(self):
        name = self.name_factory.get_name("Castro,Ricardo")

        self.assertEqual(name.get_first(), "Ricardo")
        self.assertEqual(name.get_last(), "Castro")

if __name__ == '__main__':

So, the main idea is to use unit tests to properly test our code. Like the name points out, unit tests aim at testing units of code. How can we do that?

Python provides us a unit testing framework that we can use. We can then create a class that extends unittest.TestCase and, as we previous wrote a function to test our code, we can create as many functions as we want to test. By giving a quick look at the code, two points to notice here:

  • the methods representing test cases have to start with test;
  • there’s a method called setUp. That method will be run before each test. An equivalent tearDown method will be run (if defined) after each test.

As we can see, we’re testing the output of the functions by using the assertEqual function but there’s a lot more you can do. Check the unittest for more information about the framework.

Personally, I think testing your code is extremely important. There are many advantages to testing your code properly, for example:

  • you can be sure that your code is working properly;
  • you have a way to show others that your code is working properly;
  • you can make changes to your code, and have a set of tests to make sure you haven’t broke anything. I find this particularly useful when you have to change something and you run into the fear of breaking anything inadvertently. If you have a good test suite, by running your tests you can be sure you didn’t break anything in the process.

I can think of many other reasons, but I will leave you to find them out for yourself.

Although the presented code here was written in Python, your favourite programming language should have some unit testing framework. Just search for it, read the docs and start using it. The underlying idea of this post is to write tests for your code. Like Nike: Just Do It.