Unit testing: Introduction

June 5, 2009 – 8:43 pm Tags:

Due to popular demand, I’ll be writing a bunch of posts on unit testing.

In this post I’ll introduce unit testing: What it is, when it’s a good idea and when it might not be. I’ll also discuss a bit about what makes for a good unit test.

Next week I’ll post a followup to this, which will be more about actually writing unit tests with examples.

What is unit testing

If you are already familiar with the concept, you may wish to skip this chapter.

Simply put, unit testing means writing code to test other code. There are a variety of libraries for testing for various languages: for PHP there are PHPUnit and SimpleTest, for Java there’s JUnit, NUnit for .NET Framework languages, etc.

The idea is that when you write tests for your code using one of these tools, you can easily automate them and run them as often as needed. By writing unit tests, you can easily ensure that your code works as expected, even if the code is later changed, as your tests stay the same and you can (and should) always run them after doing changes.

There’s a good article on unit testing in wikipedia, which you should check out for a more in-depth explanation of it.

When is unit testing a good idea?

There’s a short answer to this: Always.

And there’s also a long answer to this: It depends on how complex your project is, and how long is its estimated lifetime.

If you have a very simple project, writing unit tests for it may take time that would be better spent in just writing the project itself. If it’s simple, it’s less likely to be difficult to test and it’ll be easier to find errors by manually using it.

If your project’s estimated lifetime is short, you may again want to not spend time writing tests. If the lifetime is short, you usually won’t have to maintain it or do any changes to the code, making unit tests less beneficial.

That being said, if you have the time and resources to write unit tests for even small/short projects, it may be a good idea. It largely depends on the situation, and you should make the call based on your specific circumstances, but the two cases I mentioned should serve as good examples.

Writing good unit tests

The idea of a unit test is to test the behavior of a piece of code. This is usually a class and some method of it.

When writing tests, you usually would want to write one test per behavior: Expected behavior of code when given correct values and expected behavior of code when given incorrect values are the essential ones you will want to write. Depending on the case, there may be more tests that you’ll want to write, but these two are good ones to start with.

A common metric for determining how good a set of unit tests are is code coverage. Code coverage is determined by checking how much of the total code your unit tests execute – does each if and else block get executed by some test, does every function get called, etc.

While code coverage is a quite good metric, it can be misleading. Even if your tests cover 100% of your code, they may not actually test each behavior correctly.

I’ll show you some examples of good tests and testing behaviors next week.

In closing

Unit testing is a great way to improve code quality – correctness, but also modularity, because it forces you to write code in a way that it’s reusable and flexible, as it would otherwise be difficult to test.

In the next part we will take a more hands-on coding approach and I’ll show you some practical examples of writing unit tests.

Share this:
  1. 22 Responses to “Unit testing: Introduction”

  2. Awesome Jani, can’t wait for the next parts!!

    I guess the examples will be for PHP, so which lib will you use, PHPUnit or SimpleTest? Just to read up on it a bit, I wanna come prepared to our next “class” ^_^

    By Robert on Jun 5, 2009

  3. Examples in upcoming posts will use PHPUnit

    By Jani Hartikainen on Jun 5, 2009

  4. Hi Jani,

    Very good introduction. I’m looking forward to your next post and some practical examples of unit testing. Keep up the good work. :)

    By Milan on Jun 5, 2009

  5. Excellent stuff Jani. Unit testing is something I’ve been meaning to do for ages but I don’t seem to be able to find the time. I reckon this will give me a nudge in the right direction.

    Will you be covering Zend_Test in your future posts?

    By Mediablitz on Jun 5, 2009

  6. Nice intro and intresting topic, im looking forward to next posts.

    Good work


    By Bernie on Jun 5, 2009

  7. Another fan signing in, looking forward to next week.

    By Jon on Jun 5, 2009

  8. Thanks for the good explanation about Unit testing. Now I get the concept. Looking forward for the next article :)

    By Permana Jayanta on Jun 5, 2009

  9. In my experience unit tests don’t cost extra time – they already save you time and resources when first writing the code, unless you are fine with code with lots of defects.

    By Manuel on Jun 6, 2009

  10. I’d be especially interested in how to do unit testing for parts of MVC applications.

    By Thomas on Jun 8, 2009

  11. Thanks for the post. I think there is more that we can leverage from unit tests than just ensuring the functional correctness of your code. check out the following link http://blog.dynatrace.com/tag/unit-testing/ where I discuss the principle of Continuous Performance Management with the use of Unit Tests

    By Andreas Grabner on Jun 8, 2009

  12. thanks,it seems that u have a lot of knowladge abot that topic,i have just started to learn all that world and i want to try find more info so please give me more information :)

    By unit testing on Jun 24, 2010

  13. Hi, i am new to testing field and after reading your post i must say it is one of the interesting thing that i have doing. Thanks for your great post. Hope you’ll help me out in your next posts also by giving me more on unit testing.

    By Unit Testing on Jul 29, 2010

  14. Where’s my pie! Bake me a pie! I mean… thanks for this article. This is the perfect guide to get me started :)

    By Solow on Aug 6, 2010

  1. 9 Trackback(s)

  2. Jun 8, 2009: Jani Hartikainen’s Blog: Unit testing: Introduction | Webs Developer
  3. Jun 10, 2009: Unit testing part 2: Writing and running tests | CodeUtopia
  4. Jun 10, 2009: Jani Hartikainen’s Blog: Unit testing part 2: Writing and running tests | Webs Developer
  5. Jun 11, 2009: Jani Hartikainen’s Blog: Unit testing part 2: Writing and running tests
  6. Jun 17, 2009: Unit testing: Introduction | Dailytuts.net
  7. Jun 17, 2009: Unit testing 3: Writing tests for existing code | CodeUtopia
  8. Jul 8, 2009: Bookmarks for July 6th through July 7th | Maximi maxima maxi max ist da
  9. Aug 17, 2009: Unit-testing essentials | CodeUtopia
  10. Nov 26, 2009: Unit testing in PHP | Behind The View

Post a Comment

You can use some HTML (a, em, strong, etc.). If you want to post code, use <pre lang="PHP">code here</pre> (you can replace PHP with the language you are posting)