How to Write Your First Unit Test

What is a unit test?

A unit test is code that is written to test source code.  Its purpose is to run very quickly.  After you make a change to source code, you can run unit tests to make sure you have not broken any functionality in your code.  It is similar to double-entry bookkeeping in accounting or double-checking your work on a test.  Each time you run the unit tests, they go through a series of steps and check them off as passing or failing.  This is very helpful in preventing bugs from occurring in your code.

Checklist

So let’s get started!

Tools Needed

A computer with the following:

  • Python (try the latest version if you don’t have it already, but most versions will work – if you have it already, try it and see if it works first before getting another version)
  • nose (should be installable through Python pip)
  • Some sort of code editor (if you have Eclipse, Visual Studio, or something similar, great.  If not, try Notepad++).

The Code

Copy the following code samples into your code editor and save it as unittest_source_code.py and test_unittest_source_code.py.

Running the Unit Test

Within a terminal/command prompt, change to the directory of those files. Run the command “nosetests”.  You should see something similar to the following:

This is saying that Python found one unit test, and it passed.

So What Just Happened?

Questions and Answers

The source code file has one function in it that checks if one string is within another string.  If you understand Python, this is a very simple function (that honestly isn’t really needed, but this is just for sake of example).

The unit test file first imports the source code – that is what we will be testing.  It then imports the Python’s unit test framework.  Most languages have a unit testing framework (cppunit, junit, qtest are just a few).  PyUnit is one of Python’s unit testing frameworks.  We then follow the format of that test framework:

  • Create a class that inherits from TestCase
  • Make the unit test function name start with “test” (this is for the nosetests runner… every test case function must contain the word ‘test’ in it)
  • Write the test (using assertTrue to make sure whatever is within it returns True)

The nosetests command found the unit test and ran it.  Let’s now try an example with a bug.

An Example of Inserting a Bug

Replace the existing unittest_source_code.py file with the following:

Now run nosetests within the command prompt/terminal again.  You should see the following:

This is nosetest telling us that a test failed (we get an ‘F’ rather than a ‘.’).  It then gives us the name of the test and why it failed (we got False rather than the expected True value).

What’s the Big Deal?

This was a trivial example just to get you started.  Suppose rather than one function, you have a codebase that has 10,000 files and hundreds of thousands of lines of code.  You also have unit tests that test most of the functionality of that system.

With that set of tests, you can now change a whole lot in that code without worrying about breaking something.  It is an awesome feeling not having to test so much and having those tests protect you from creating bugs.

Even better, you can make these tests run automatically every time someone commits code through something called ‘continuous integration’.  Tools that provide that feature also usually allow you to send out emails when the tests break.  So you have a system that tells developers automatically when they broke code!

Leave a Reply

Your email address will not be published. Required fields are marked *