Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:
JavaWorld Daily Brew

Test-Driven Development and Software Quality


 

Any Test-Driven Development practitioner will tell you, Test-Driven Development is a design strategy, not a unit-testing technique. Writing unit tests are a means, not an end. The goal is to write better quality, more reliable, and more accurate code.

I recently read a study in the IEEE Software journal ("Does Test-Driven Development Really Improve Software Design Quality", IEEE Software, March/April 2008), by David Janzen and Hossein Saiedian, that tried to establish measurable benefits of Test-Driven Development on software design quality. Now any TDD practitioner will tell you - using TDD properly tends to produce better quality code. Not just code that is tested better, but well-designed code.

This study looked at TDD as an isolated practice, independent of other agile practices. Indeed, TDD isn't limited to Agile development - you can also do TDD in more traditional development processes. Just replace the "Detailed Design -> Code -> Unit Test" cycle with "Unit Test -> Code -> Refactor".

Not suprisingly, teams that practiced TDD obtained much better code coverage statistics. But, more interestingly, the study also found that coding teams that practiced TDD tended to implement smaller, simpler solutions, with less lines of code overall, and less lines of code per method. The cyclometric complexity was also lower, which is a side-effect of simpler, more testable code. My own experience, like a lot of TDD practitioners, tends to bear these finding out. However, it is hard to come up with objective evidence, since you rarely solve exactly the same coding problem in the same conditions twice.

Testable classes tend to be more modular, configurable and flexible. My gut feeling tells me that classes coming out of a TDD approach are more loosely coupled. TDD is a nice fit for techniques such as Dependency Injection and the use of interfaces, which encourage more loosely-coupled and more flexible components. The study did not provide any hard evidence of this, however. The authors speculated that this reflected the fact that TDD developers use a "good" kind of coupling, using interfaces and abstract classes. This "good" coupling results in more flexible components, rather than more rigid applications.

So, basically, there is some objective data indicating that TDD promotes better quality code. This makes sense. When you practice Test-Driven Development, you think about how your class will be used by other classes. Go far enough and you should get to a precise user requirement - otherwise, what are you coding for? Just writing code without any thought to design or testing is just hacking. When you do traditional up-front design on paper before you code, you won't think of everything, and the original design often bears little ressemblance to the final implementation. On the other hand, writing, or at least envisaging, your unit tests helps you flesh out a clean and efficient design for your class. You can jot down UML sketches to help formalise your ideas, but there is no substitute for trying to write the code that actually uses your class.

One interesting exception is the domain model, which often lends itself well to careful up-front modelling, though even this will evolve throughout the life of the project.

TDD - other opinions

Hi John,
what do you think of the "anti-arguments" offered by Cedric Beust, TestNG creator, in his blog?

TDD - not for every project?

Good question, Klaus. Beust's main point about TDD is that it doesn't work for all developers in all domains. As he says, "There are a lot of factors that make this practice a bad fit for a lot of projects and developer personalities (aspects which are very often never mentioned by TDD extremists)."

Based on your experience, what types of projects do not benefit from TDD? And, if code quality is the goal (and you agree that TDD isn't the only solution) what are some reasonable alternatives?

TDD and SW quality

Hi John,
in my daily programming and testing practice I follow Cedric Beust's 2 rules of thumb:

# "Tests first" or "tests last" is unimportant as long as there are tests.

# Try to think about testing as early as possible in your development process.

We are developing the agile way (using Scrum) in the project I'm involved for the last four years. During that time the management arranged 2 special TDD trainings but as far as I know only a part of the development team strictly follows TDD practice. IMO it is a question of the "mindset". I have read some artciles on TDD but the examples given where just to trivial to convince me, especially when I think of our complicated telecom environment that is the project's domain.
I also looked at the book Agile Java that introduces TDD, but again, the examples we OK but not suited to our complicated domain. But perhaps it is not really a problem of our domain; it may be a problem of time allocation and mindset ;-)

I'm really feeling constrained to software and test code quality, but I think it is not necessary to perform TDD to reach that goal. We have to aim for good architecture, good design, and good code: by means of Code Quality Management, static code analyzers, design & architecture checkers, by simply using the features of our IDE to detect problems during coding, etc. AND we have to study and improve existing test code as well, e.g., using the methods described in the excellent book XUnit test patterns written by G. Meszaros.

The other thing we can do is to look for the best testing environment. We are using JUnit 3.8.2 because at the time we started, there was no JUnit 4 available. So I wrote a lot of JUnit add-on-tooling and we used JUnit extensions. Today, when starting a new project, I would use TestNG. We also experimented with AgitarOne but as you might have read, the company is winding down operations...
For the automation process we are using Ant and Groovy/Gant. Our performance test is based on Open Source Grinder and on the commercial LoadRunner tool. Because we are testing the Java API of our framework (system tests based on JUnit) and of Web services too, we are able to even re-use part of our functional JUnit system tests.

tdd and code quality

I've been actively practising tdd for several years now and mostly agree with the blog entry. What I've found is that in addition to tdd, you must be aggressive with refactoring and code simplification. Remove duplication wherever you see it including in your unit tests.

I have code coverage levels of 99.8% on 2 different projects but both of them are harder to maintain than I expected because I've let various classes get too complex. While these classes are still far easier to maintain than if I had no unit tests, they are still harder than I expected to maintain and I suspect it is the complexity of the code.

It could be the problem domain is complex, so maybe i'll get it refactored and simpler and still find it takes longer than I want it to to maintain but that is my current challenge.

TDD and unit test refactoring

Test coverage of 99,8 % is impressive but do you know this nice article In pursuit of code quality: Don't be fooled by the coverage report ? ;-)
In the past I also fighted with code duplication, in the development or in the test area. Duplicated code is one of the classical "code smells" that cry for refactoring. I even installed a duplicated code finder Eclipse plug-in but my experience is that most of the people do not really care about this smell.
Perhaps you know the book xUnit Test Patterns: Refactoring Test Code that has a lot of good advice and refactoring tips for test code.
But you definitely need developers and testers who are really commited to software quality -- even it is not requested every week in management Powerpoint slides...

I also looked at the book

I also looked at the book Agile Java that introduces TDD, but again, the examples we OK but not suited to our complicated domain. But perhaps it is not really a problem of our domain; it may be a problem of time allocation and mindset.
Lighting Fixtures

Yes! Indeed

Yes! Indeed the other thing we should always do is to look for the best testing environment. We are using JUnit 3.8.2 because at the time we started, there was no JUnit 4 available. Buy Cialis So I wrote a lot Last Minute of JUnit add-on-tooling and we used JUnit extensions. Today, when starting a new project, I would use TestNG. We also experimented with AgitarOne but as you might have read, the company is winding down operations...

Based on your experience,

Based on your experience, what types of projects do not benefit from TDD? And, if code quality is the goal (and you agree that TDD isn't the only solution) what are some reasonable alternatives?

It's Cyclomatic not Cyclometric

The other thing we can do is

The other thing we can do is to look for the best testing environment. We are using JUnit 3.8.2 because at the time we started, there was no JUnit 4 available. So I wrote a lot Last Minute of JUnit add-on-tooling and we used JUnit extensions. Today, when starting a new project, I would use TestNG. We also experimented with AgitarOne but as you might have read, the company is winding down operations...

tdd and code quality

I've been actively practising tdd for several years now and mostly agree with the blog entry. What I've found is that in addition to tdd, you must be aggressive with refactoring and code simplification. Remove duplication wherever you see it including in your unit tests.

Running a series of systems

Running a series of systems testing have proven to be among the best practices. This way, the developer can see how the system would fare under varying actual scenarios. Alvin at folding chairs station

Although there are lot of

Although there are lot of factors involve in using TDD but I think performing TDD is necessary so that the developer can see how the system would run under different circumstances.

Great site, love whats going

Great site, love whats going on
shock collars for dogs

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.