Showing posts with label testing. Show all posts
Showing posts with label testing. Show all posts

Thursday, June 10, 2010

Having GUTs

Writing Good Unit Tests plays an important, proven role in improving and maintaining code quality and productivity. Still, quite many developers haven't embraced it. Based on experience (have tried it and failed) or haven't even considered trying it in the first place. Probably had their reasons, in any case I find it very interesting that developers who have successfully practiced unit-testing stick to it and rarely go back. This should at least cast a question on those reasons and make one think about the cause for the initial disbelief and failed attempts. My intention is to collect a few practices that address common pitfalls and show some of the actual value added by unit tests.

Test the testable only

Test testable code only. What code is testable? Potentially everything inside the system's boundaries. Outside that there are the external dependencies, which generally don't need unit testing. Identifying the system boundaries and separating it well from external dependencies is a key element. This separation also tends to lead to better overall design. The ratio of testable code depends on the concrete piece of software: it can be low in some cases.

Example code

The example code we aim to test implements a Queue.
public class Queue<T> {
    private LinkedList<T> queue;

    public Queue() {
        queue = new LinkedList<T>();
    }

    public void add(T obj) {
        if(obj == null)
            throw new NullPointerException();
        queue.add(obj);
    }

    public T poll() {
        T first = queue.getFirst();
        queue.removeFirst();
        return first;
    }

    public int size() {
        return queue.size();
    }
}

A bad testcase

"Public methods of all public classes should be tested." I read this in a best practices list. I think guidelines like this are confusing and don't focus on the issue at all. Let's take a look at a naive attempt to follow such an advice.
public class QueueBadTest {
    @Test
    public void constructor() {
        Queue<Integer> queue = new Queue<Integer>();
        Assert.assertEquals(0, queue.size());
    }

    @Test
    public void add() {
        Integer i = 5;
        Queue<Integer> queue = new Queue<Integer>();
        queue.add(i);
        Assert.assertEquals(1, queue.size());
        Assert.assertEquals(i, queue.poll());
 }

    @Test
    public void poll() {
        Integer i = 5;
        Queue<Integer> queue = new Queue<Integer>();
        queue.add(i);
        Assert.assertEquals(i, queue.poll());
        Assert.assertEquals(0, queue.size());
    }

    @Test
    public void size() {
        Integer i = 5;
        Queue<Integer> queue = new Queue<Integer>();
        queue.add(i);
        Assert.assertEquals(1, queue.size());
        queue.poll();
        Assert.assertEquals(0, queue.size());
    }
}

This testing style fails for a number of reasons. It is obviously impossible to test methods (sometimes even classes) in isolation. The constructor test depends on the correctness of size(), all other tests depend on the constructor and a few other methods too. The test for poll() doesn't test polling from an empty queue for instance. poll2()? Or making the existing test method more busy? Circular test dependencies can potentially make the whole testcase worthless anyway.

Unfortunately there are many tools that generate such testcases and advocate this style. It parrots the class interface, doesn't achieve it's goal and rightfully gives the impression of more work without much added value. We're looking for something more useful.

Identify the unit

A unit must be identified first. A unit test is not called a class test, there is a reason for that. Anything can be considered as a Unit as long as it remains in the system's boundary. Trying to write isolated class tests may not be practical in some scenarios. Fine-grained units are to be preferred of course, so the test points to the issues more accurately.

Concentrate on requirements

When the unit is identified we should ask what behaviour we expect from it? Returning to the Queue example, here is a possible requirement list for our Queue:

  • queue is empty when created

  • accepts non-null elements for adding

  • elements can be polled (if not empty)

  • values and order of elements must not change (when adding - polling)

  • can tell the number of contained elements

Requirement-driven testcase

An expressive, explicit testcase can be built based on this requirement list.
public class QueueBetterTest {
    @Test
    public void emptyOnCreation() {
        Queue<Integer> queue = new Queue<Integer>();
        Assert.assertEquals(queue.size(), 0);
    }

    @Test(expected = NoSuchElementException.class)
    public void emptyPollShouldFail() {
        Queue<Integer> queue = new Queue<Integer>();
        queue.poll();
    }

    @Test(expected = NullPointerException.class)
    public void addingNullElementShouldFail() {
        Queue<Integer> queue = new Queue<Integer>();
        queue.add(null);    
    }

    @Test
    public void keepsValuesAndOrderOfElements() {
        Integer first = 1;
        Integer second = 2;
        Integer third = 3;
        
        Queue<Integer> queue = new Queue<Integer>();
        queue.add(first);
        queue.add(second);
        queue.add(third);
        Assert.assertEquals(queue.poll(), first);
        Assert.assertEquals(queue.poll(), second);
        Assert.assertEquals(queue.poll(), third);
    }

    @Test
    public void sizeMaintained() {
        Queue<Integer> queue = new Queue<Integer>();
        queue.add(1);
        Assert.assertEquals(1, queue.size());
        queue.poll();
        Assert.assertEquals(0, queue.size());
    }
}

The value

Capturing the requirements in code and having them checked automatically saves a lot of development effort continously and provides a lot of confidence and safety for making changes.

Notice that test code uses the class in a similar way as client code: this provides documentation and early feedback on the design. If writing a test is difficult, the cause may be a design flaw. Test code is ugly because of interface flaws? Client code won't look any prettier. Class depends on too many other classes? Maybe they could be decoupled some more. No sense for writing test for "dumb" classes without logic? Testing their overly complex controller classes is difficult? These are all most useful hints to drive design.

Thursday, May 6, 2010

Why invest in TDD? Refactoring.

I've been asked lately why test-driven development so important. Apparently many teams (or their management) still think of methodical unit testing as an additional luxury expense. "We don't have time for tests", "Test code can't be sold" are common arguments.

Without digging into testing methodology, design considerations and consequences, I'd like to focus on benefits in the broader context of development process. Most modern methods - especially agile - aim to address the problem of code decay. During a software project's life-cycle the code quality deteriorates, and the cost of introducing new features increases significantly. Refactoring is the main means to improve code quality and protect against decay in order to keep development and maintenance costs manageable.

Note: this diagram is an illustration
We can come to the conclusion that continous refactoring should be an organic part of the development process. The main challange is though, non-trivial refactorings are potentially unsafe. Even a carefully done refactoring has potential to breaking functionality. Most developers - rightly - don't do unsafe operations often (otherwise they would sit in front of a debugger all day long without being productive). One of the most important goals of testing is to make refactoring safe: make resulting problems instantly apparent and easy to pinpoint. According to my experience they serve this purpose very well.

I highly recommend the following book for reading: Refactoring - Improving the Design of Existing Code from Martin Fowler. He discusses this topic in great depth.