Code Elegance

Archive for May, 2007

What to test?

When you write a Unit Test, you may ask if it’s good or not test some functionality. A classic example is “Should I test all getters&setters?”. The simple answer is no, since the gets and sets doesn’t contains any logic. 

Some people may say to write test for everything, even the get and set, because if now they are simple accessor in the future they can become more complex (i.e. doing some sort of validation or parsing). This can be truwe but it breaks one of the law of the agiles methodologies “don’t do today what you can do tomorrow”.

My opinion is that a test should test only methods that contains application logic.

 Another doubt is: “Should I test private methods?”

Even this is not a simple question. I think that a private method should not be tested becouse it is part of internal implementation of that class, we should test the interface (public methods). This because if i’d like to refactor an implementation of a private method I’d like to  be free to change it whatever I like, without break the tests.

In general the rules to follow are:

  1. Test that the method do exacly what it was write to do, that the results are correct.

  2. Test the extremes. Often a method is tested with normal inputs, but it’s better have some test that use “special values”. For example to a method that get a String try pass an empty String. If it get an integer, try pass 0, -1, Int32.MaxValue.

  3. Test the errors. Your code throw an exception when the parameters are wrong? Test it. Verify that this is true.

  4. Make the test atomic. The single test shouold verify only one simple funcionality because if this test become red you can get the bug quickly.

No comments

Ehi Microsoft, please be quite…

Don’t you think that Microsoft is working on a lot of new product? I’m a big user of Microsoft’s software, I’m a developer, I like the cutting edge technology.

Take Visual Studio 2005 for example. Do you think ti is a great product? I don’t think so. I crash a too much times, it lacks of features (and I have to install 3rd parties addins). But at the end of 2007 Orcas will be here.

Don’t you think that is better to have a real service pack for Vs2005?

No comments