Most Read This Week
Applying a Java Unit Testing Framework
Applying a Java Unit Testing Framework
By: Java News Desk
Oct. 5, 2006 11:30 AM
Most software developers have heard about the benefits of Java unit testing frameworks, and may have implemented unit testing at a previous job; but tight schedules leave little time for such initiatives; and isn't it Quality Assurance's job to identify bugs?
In fact, identifying and correcting bugs as early as possible in the development cycle reduces overall bugs, freeing developers for more creative work. It also improves team cohesion and minimizes the frustration and anxiety common with an endless cycle of bug fixing.
The Cost of Bad Code
It's a well-known fact that the cost of fixing a bug significantly increases the further along in the release cycle it is detected. Doesn't it make sense, then, to implement development methodologies that prevent bugs from being created in the first place? But what are these methodologies, how can they help, and how can they be most successfully applied?
Software Development Trends
Within any development team there are a variety of skill sets, with the least skilled often taking a great deal of everyone else's valuable time.
What's worse, more junior developers can potentially delay or halt the entire project by contributing buggy or poorly tested code.
Many Java teams are turning to agile software development techniques, an iterative and incremental approach to software development that is performed in a highly collaborative manner. One agile software development practice is to test early and often, with the focus on test-driven development, where teams work in short cycles writing tests for a piece of code first, then writing the code to be tested. The key is to write tests before the code, with the objective of getting as close as possible to 100 percent test coverage.4
Unit Testing: What Good Is It?
However, in real life, almost the only significant metric that is commonly applied to the software development process is whether or not a team is hitting its deadlines. Under pressure to deliver, most developers will focus on getting their code written, and let the Quality Assurance (QA) team uncover any bugs. And there will be bugs. Yet, the purpose of QA is not really intended to be finding bugs. They are responsible for:
Most developers have heard about unit testing and agree that it should be done; but claim that they can never find the time, and that management doesn't consider it a priority. Some developers, faced with tight deadlines, can't comprehend the possibility of starting to introduce unit tests to an existing body of code. Along with documentation, it is the least important item on their list. Testing code is boring, especially when it's someone else's code.
So why should developers change the way they work? Why should they take the time to unit test? For years, unit testing was relegated to the "I know I should be doing it" category; but now it has become an important part of a top-notch Java developer's toolkit. Unit testing gives developers the confidence that their code works and that they can change it at a moments notice, without fearing that it will break.
With the trend toward agile software development, being the best programmer on the team is not enough. Agile software development is not just a fad. Its precepts of teamwork and unit testing have been shown to significantly improve code quality, affording developers more time to write code; and decreasing the time they have to spend struggling to fix or enhance bad code. The bottom line benefit is cost reduction.
Applying a Process Change Template
The following steps, that provide a template for a quality-driven development process, can be applied to unit testing to provide a manageable path to implementation:
First, organizations need to identify where they are. Are they using a unit-testing framework? If so, what percentage of code is covered by unit tests, and what is their goal?
To facilitate trend tracking, the highest level of this template approach, each step must be built upon the last. It is not necessary, however, to halt all development in order to implement this template. Organizations with a large installed code base, for example, may want to ensure that all new work is unit tested, so that over time, code coverage steadily increases.
Standards – Level 1
Another key metric related to unit testing is the pass rate—the unit test pass/fail ratio. Is it important that all unit tests pass? What if 100 percent of unit tests pass, yet these tests only cover 20 percent of the code base? Moving toward 100 percent for both metrics would be a better goal.
Tools – Level 2
The primary reason for using a testing framework is to make developers' lives easier, by identifying bugs early in the development cycle, when they cost less to fix. These tools do not necessarily have to be standardized across development teams, because some developers may have experience in and prefer using certain products.
Unit Testing Alternatives
Inserting debug statements into code is a low-tech debugging method. It usually requires that you scan output manually every time you run the program to ensure that the code is doing what you expect.
Debuggers are used to step through code and inspect that the variables along the way contain the expected values. This is also a manual process that requires tedious visual inspections. Every time you change your program, you must repeat this manual process.
More than any other testing framework, JUnit has helped developers understand the benefits gained from consistent unit testing.
JUnit features include:
The intention of a testing framework is to write tests before the code. This is called test-first programming, whereby new code is only written when an automated test fails. Good tests will show how to best design the system for its intended use.
Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Today's Top Reads