Good software is reliable software. It rarely crashes or does strange things to the user. Making sure it’s reliable takes more than running the finished product through its paces. There are too many possible combinations of circumstances to test them all. That’s the reason for the software development technique called unit testing.
Unit testing catches more bugs
Computer code forms a hierarchy. Top-level routines call lower-level ones, and they call ones that are still further down. At the bottom level are pieces of code that do simple things, but there are lots of them. One that is always called and never works will be easy to catch. Other routines, though, don’t get called very often. Or they get called a lot, and they work except in a few rare cases. Those cases will turn up sooner or later, but they can stay in hiding for a long time and only raise their heads when the product ships. It’s Murphy’s law. Anything that can go wrong, will go wrong, so make sure nothing can go wrong.
Unit testing helps to fight Murphy. It’s the technique of developing a set of tests for each little piece and running them each time the code changes. The tests will fight the code as hard as possible, giving them parameters that are likely to break it. If the test catches problems, it will report them to the programmer.
In a strict unit testing environment, the developer has to create a test for each piece of code. If the code just does a calculation, that’s straightforward. Other types of unit tests are trickier and may require simulated environments. Unit testing code that accesses a database will need a simulated or test database. Dummy data will feed tests that need user input.
Free Project Cost Estimation Tool
Download this free Microsoft Excel Project Estimator tool to gain some insights into what your software development project might cost! Fill out the form at the right to download this FREE resource.
Approaches to unit testing
Developers have different views on unit testing. Some insist not only that every subroutine should have a test, but that the test needs to be written along with the code from the beginning. This is called test-driven development. In this approach, you start with a test that always fails, because the code doesn’t do anything yet. You write the simplest part of the test and make sure the code passes that. Then you enhance both the code and the test until the code does what it’s supposed to and always passes the test. Test-driven development tests everything, all the time.
Some developers think this is a little too fanatical. This doesn’t mean they leave anything untested, but they opt for other kinds of tests. Not every piece of code performs a self-contained function. Some code has complex interactions with other code and is hard to isolate into a unit. Integration testing works with components or subsystems. It avoids having to create a self-contained environment for each subroutine. Which way is better is a subject of constant, furious debate.
Whatever approach developers take, they need to build testing into their plan from the beginning. When developers realize halfway through a project that they should add testing, it just doesn’t work. Building test frameworks retroactively is painful work. The result is usually that tests get added just to the simplest parts, which are least in need of testing. It’s better than nothing, but not by much.
Rigorous testing at the beginning may seem like extra work that slows down development, but it shows its worth later on. As time passes, the code gets more complicated, and when something goes wrong, it’s hard to spot which piece is to blame. If the low-level code is tested and the developers have confidence in it, then the high-level code will have fewer unexpected problems.
We take pride in building software right. Get in touch with us if you’ve got a project in mind, and let’s talk about it.
Free Project Cost Estimation Tool
Download this free Microsoft Excel Project Estimator tool to gain some insights into what your software development project might cost! Fill out the form at the right to download this FREE resource.