Viewpoints
Testing, Testing . . .
Testing, Testing . . .
Jun. 1, 2003 12:00 AM
While I was preparing for my interview with Bruce Eckel, a quote appeared in his Web log in May that said "If it's not tested, it's broken." It got me thinking about how much I actually tested the code that I wrote. Now I don't write JUnit tests for everything, but perhaps I should. To that end, here's my proposal (I'm looking forward to the royalty check):
1. Sun should embrace the JUnit framework and include it in the Java Development Kit.
2. Any untested methods should throw an UntestedMethodException.
If that doesn't tell you how much that download has been tested, I don't know what will. Now I appreciate that there will be developer overheads in creating test cases, but perhaps it may make the job title "Test Engineer" popular again. This will decrease the number of unemployed IT staff; everyone's happy - apart from developers.
It's time to face facts, ladies and gentlemen; we got away with it for far too long. Now it's time to change. Let me first reassure you that "change" is not a dirty word; in fact it's one of the best things that can happen to you. Thinking differently forces you to apply yourself differently; take note of the implications of that change, and create a more secure, tight, and successful way of working. You feel good, the team feels good, and then management sees it and they feel good; company profits go up, the board and the shareholders feel good. All that from one small change. Only you control your destiny so make it work for you. Insist on change.
Anyway, back to testing. The time has come to put our former ways of testing behind us and look properly at what we do. Take the following example:
public class Calc {
public Calc(){}
public add(int a, int b){
return a+b;
}
public subtract(int a, int b){
return a-b;
}
public static void main(String[] args){
Calc c = new Calc();
System.out.println(c.add(1,2));
System.out.println(c.subtract(5,2));
// ooh! It works :)
}
}
It's very easy to assume that this class has been tested and what I've been guilty of (I can only assume countless others as well; I just hope someone else confesses :-) ) is accepting in my mind that it's okay. What we really should be doing is supplying proof to everyone else that it works! I don't want JUnit tests to be optional; I want them to be mandatory! Eclipse does a nice job of creating unit tests, but it would be nice if Eclipse (or insert the IDE of your choice) could monitor the methods and create unit tests for them automatically. Then all you'd have to do is create some test data and run the tests. Easy, simple, and you'd have evidence.
It's all too easy to fall into the trap of delivering code that you think is okay, but in no time at all management is at your desk demanding to know why the application is not working. What evidence do you have with no unit tests? Effectively none. Now I openly admit that I have sat on the fence for far too long concerning this, and I think it's one of the reasons that management spits fire and loathes some IT departments. At the end of the day there is only one person who can change my predicament - me.
Let's put down our lattes/smoothies/stock options and start writing some proper unit tests, with decent test data and proper outcomes. I'm fed up with the stigma. If you want more info on JUnit, have a look at www.junit.org. There's also an article in JDJ called "Test First, Code Later" by Thomas Hammell and Robert Nettleton (Vol. 7, issue 2) that's excellent as well.
About Jason BellJason Bell is founder of Aerleasing, a B2B auction site for the airline industry. He has been involved in numerous business intelligence companies and start ups and is based in Northern Ireland. Jason can be contacted at jasonbell@sys-con.com.
#6 |
dionn commented on 11 Jun 2003
Martin Fowler also discusses the importance of testing -- early and often -- as he describes "Continuous Integration". (See his article at http://www.martinfowler.com/articles/continuousIntegration.html)
At IronGrid, we've embraced that concept as well and followed on specifically with "Continuous Performance" which focuses on (you guessed it) performance testing at every build -- early and often. Check the whitepaper "Continuous Performance: The Next Advance in Software Development" at www.irongrid.com.
|
#5 |
Bill, I completely agree with what you say... The source code example does show the point I was trying to get across, that people are willing to use a main() method as a valid testing case. This is seriously wrong.
|
#4 |
Bill Galen commented on 11 Jun 2003
public class Calc {
public Calc() {
}
public int add( int a, int b ) {
return a + b;
}
public int subtract( int a, int b ) {
return a - b;
}
public static void main( String[] args ) {
Calc c = new Calc();
System.out.println( c.add( 2000000000, 2000000000 ) );
System.out.println( c.subtract( 5, 2 ) );
// ooh! It works :)
// -294967296
// 3
// oops! It doesn't work :( The code you don't write has the fewest bugs.
// If you depend on tests, write dependable tests.
}
}
|
#3 |
I loved the article! I just recently graduated from college. I am very lucky to have a boss-(technical teacher) to take me under his wing, and show me the way to develop software. Everything begins with the test, or atleast it should. Writing the software is easy, making it work is the hard part. But, if you test the code before you write it, then putting it together ensures the stability of the code. It also helps save time in the development process. If a programmer can test his/her code first, then this will save them time debugging their code later. I use Netbeans JUnit IDE to write and test my code. I write out a class with empty methods and create JUnit tests from them. the JUnit methods are all set to a failed state, until you write your code for the method. It is somewhat of the same format as the unTestedMethod format used in the article.
|
#2 |
Ben Butchart commented on 6 Jun 2003
I think it is right that the secret to testing is adopting the right mind set. I agree with Kent Beck and others that the best way to write test code is to write the test before the code you are going to test. This might sound crazy at first but it makes alot of sense. First it makes you more honest. If you've already written and compiled code you tend to lose interest. The fun bit of working out how to do it and making it compile and run is over. Writing a test is boring after that. Writing the test first is just as challenging as writing the code itself though and you can be honest and imaginative thinking up test cases because you haven't invested any time or pride in what you are testing yet. Also you tend to design code to be more reusable. Usually when you design a class you have a particular client and use case in mind - it's hard to keep the interface general with that requirement in mind. But your test provides another client and another use case, forcing you to think more generally about the class you are writing. On the whole I've found this shift towards test first code has improved my productivity and the quality of my work considerably.
|
#1 |
Fred Brassard commented on 5 Jun 2003
It is nice to here that a developer dose admits to not testing enough. As a QA engineer that has seen development think that test all of their code, and does not need QA, this is good. With industry laying off most of their QA engineers in order to get the product out early. If they would have QA start at the specification level their product would get out the door sooner.
|