Why Writing Testable Code is Important

Main |

As discussed previously, testing your code is important and thus writing code that can easily be tested is even more important.  This will however focus on the automated form of testing using unit testing for testing specific chunks of the code in isolation.  One of the greatest problems when writing test cases for very specific sections of code is that it can prove near impossible if the code relies on hard coded dependencies.

Imagine we are dealing with a chunk of code that as part of its functionality calls a web service and uses a value from that, we wouldn’t want calls to that web service to be made during our unit testing as that would take far too long and would rely on that web service being up and running properly and a network connection to be present during testing.  The code for that could look like the below example code in PHP.

 

function checkLatestNumbers() {

     $webService = new WebService();

     // All numbers are non-negative integers.

     $numbers = $webService->getLatestNumbers();

     $numbersSize = count($numbers);

     if($numbersSize<2)

return false;

     $numbersSeen = array();

     for($i = 0; $i < $numbersSize; $i++) {

           if($numbers[$i]>10)

                continue;

           if(isset($numbersSeen[10-$numbers[$i]]))

                return true;

           $numbersSeen[$numbers[$i]] = true;

     }

     return false;

}

            Testing the code above would be difficult for the reasons mentioned above.  That hard coded web service dependency causes problems when we go to test this code, so we need to find a way to be able to pass in a mock web service class. Using a mock web service class we don’t need to touch the network and talk to a web service in order for this to work and to make sure that this function can indeed see if any two of the numbers obtained do in fact sum to ten.  A simple way to fix this would be to have that function take a WebService class instance as a parameter rather than to create it in there directly which would allow a mock WebService class or a different implementation of the WebService class in general to be used.  With this idea in mind we could modify our example as seen below.

 

function checkLatestNumbers($webService) {

     // All numbers are non-negative integers.

     $numbers = $webService->getLatestNumbers();

     $numbersSize = count($numbers);

     if($numbersSize<2)

return false;

     $numbersSeen = array();

     for($i = 0; $i < $numbersSize; $i++) {

           if($numbers[$i]>10)

                continue;

           if(isset($numbersSeen[10-$numbers[$i]]))

                return true;

           $numbersSeen[$numbers[$i]] = true;

     }

     return false;

}

 

// Test the above function with PHPUnit now that it’s testable

class checkLatestNumbersTest extends PHPUnit_Framework_TestCase {

    public function testCheckLatestNumbersWithNoNumbers() {

        $webService = $this->getMock(“WebService”);

        $webService->expects($this->once())->method("getLatestNumbers")->will($this->returnValue(array()));

        $this->assertFalse(checkLatestNumbers($webService));

    }

    // We could have more unit tests here...

}

     Even small changes such as what we just went through above can make a dramatic difference in the testability of code.  Taking little steps like that can greatly help you put your code base under test and verify that it continues to work as expected even after future adjustments.  I hope that after reading this you will keep this in mind as you write code so you are always writing code that can be easily tested even if unit tests might not be written as this will help you to have more modular code anyways.

About the author: Preston Skupinski

RSS Feed RSS News Feed

Newsletter Signup

Talk to Us   1.800.239.8181

Categories