Getting started with unit testing

Some months ago, I decided to find out what software development is about. I had a look around in the wide web, I bought some books and I started to investigate. How do you build and maintain better software?

I found out that unit testing can be part of a good software development plan. Unit testing is a collection of procedure that test individual parts of your application. Each procedure tests a single method or a single function in your software. It is a kind of proof to assert that yours methods or yours functions do what they are supposed to do. While modifying your application, you can run your unit tests again and again to make sure that the modifications you made did not change the meaning of your functions and that your functions are still working properly.

You can also view your unit tests as contracts. You can even start writing your unit tests before you write the function you are planning to test. That way, you are specifying what the function is supposed to do.

The first test I wrote was for the login method of a user manager class. Here is what it looks like:

function testValidateLogin() {
// test login not found
assertFalse(userManager.validateLogin("userNotInDatabase", "passwordNotInDatabase"));

// test login bad password
mockDatabaseUser = { username: "user1", password: "pass" };
setMockDatabaseObject(mockDatabaseUser);
assertFalse(userManager.validateLogin("user1", "notGoodPass"));

// test login deleted
mockDatabaseUser = { username: "user1", password: "pass", deleted: true };
setMockDatabaseObject(mockDatabaseUser);
assertFalse(userManager.validateLogin("user1", "pass"));

// test login valid
mockDatabaseUser = { username: "user1", password: "pass" };
setMockDatabaseObject(mockDatabaseUser);
assertTrue(userManager.validateLogin("user1", "pass"));
}

This first unit test is managing 4 different cases that my validateLogin method must support:

  1. The case where the login does not exist in the database.
  2. The case where the password is incorrect.
  3. The case where the user was deleted.
  4. The case where there is a valid login attempt.

Since unit testing is to test individual parts of your software, testing a method that depends on other methods or that depends on external sources, you have to simulate those dependencies or those external sources when you are testing your method. In this case, I used mock objects.

Mock objects are objects whose purposes are to simulate complex objects in a simple way. For instance, in my example, I used some mock database user objects. They are not like the real ones used in my application. They are simple objects that simulates the real database user objects but only return the same value all the time and they do not read or write in the database at all.

So what is needed to get started with unit testing? First of all, you can have a look at the existing unit test frameworks for your language. They come in many different flavours, but they are mostly doing the same thing. You will also want to have some kind of mock facilities to test one single function at the time. You might also want to have some kind of build server or unit testing server that run your tests once a day and produce some nice reports about the results to follow the whole quality picture.

Unit testing can be a long process. It often requires a great deal of will in your software development environment and some exactness. It can also be a heavy process in the sense that you have to write nearly the double of code for your software, each method and each method that test your methods. In the end, if you want to create some quality software, I think it is worth it. If you have a look at some major open source project, you can probably find unit tests in every one of them. If you care about quality in your software, unit testing is a good start.

Comments 3

  1. Zaiya wrote:

    Pin my tail and call me a dkoney, that really helped.

    Posted 19 Jul 2011 at 2:38 pm
  2. ixmzbe wrote:

    V6f4us qrumecttffsy

    Posted 20 Jul 2011 at 8:11 am
  3. iivfypknve wrote:

    AHkvlz ankyozxympyh

    Posted 22 Jul 2011 at 6:38 am

Post a Comment

Your email is never published nor shared. Required fields are marked *

CAPTCHA image