So whats the hardest part of writing unit tests beyong writing the first test.
Writing the second, third, fourth …
Another important factor is that if there are not enough tests, the developers are not having the comfort zone to make changes, refactor etc. When the trust factor on unit tests goes down, developers don,t keep it up to date and soon unit tests are just dead code never to be run again. This is seen quite often in projects today. This translates to more hours debugging, wasted down times , more cost and slip in schedules.
HEre are some symptoms that your code is dying and there is not enough active healthy test coverage. I say healthy because its not enough to just check for null values and collections, its going beyond the basics, using it as a tool to write the code that make all the difference. The code is only as good as the tests are.
Symptoms
1) It takes for ever getting even small pieces of funtionality out of the door
2) Bugs that were fixed few releases ago, keep crawling up
3) There is a resistance from developers to make any kind to change to major parts of the system.
4) Your developers work crazy hours on code and have no idea when they will get finished . The only thing they know is that they are working hard.
5) There is a blame game between QC and development. Sort of a throw it over the wall mentality. Where developers ship code to QC , QC starts finding silly defects which could have been easily found during development. The stress levels are high between the two groups. Development has the big brother mentality.
Avoiding all these are quite easy if developers are motivated and unit test culture is built into the culture of your organization. Critical to this is management support and an understanding that it takes time and money to produce good tests. This is not something that can be done in as much time as just writing code. It pays of in the long run for a health y product and goes a long way during maintenance.
It is very important to keep these tests running on every build. Continuous test integration. Running all tests every check in. ( It does not take much if the tests are structured correctly)
Unit test Category tags in NUNIT ( for example ) provide a great way to seperate unit test , integration test, long running tests and so on.
The # 1 Mantra of writing tests is Test independence. Test in every layer should run without depending on the other layer. Mock objects, Object databases, in memory databases, Test driven development can really help this cause. This process actually makes the design better and allows for better encapsulated systems that are not interdependent.
Test independence means
1) Every class / component should be testable by itself
2) When you test a class, lets say in the business layer, (that calls the database layer) , you should be able to test the business layer without the database being there ie Mock the database. These may be part of your test category Unit Test
3) The database layer ( if you are unfortunate to use a relational database) should also be unit tested. This becomes part of you Integration Category.
4) You could use a mock or a stub( Look on Fowler’s site for an excellent discussion on the difference) to test dependent layers.
5) Anything that touches a physical system like a database, file system, external systems are all Unit tests but could be treated as a different category. Many time developers treat this as integration test and dont think they have to run it at all times. its your code your team is responsible for its test health and code quality.
6) Follow the 15 – 30 minute agile coding cycle .
- Get code from repository,
- Work on one task at a time ( Most men are really bad at multitasking, if you are a women then may be you can work on a few at a time)
- Write few Tests from a list of tests that you have planned to write for that task,
- Update code again from code repository,
- Build and Run all the tests ( build database if needed – if it takes too much time) then run all unit tests at category Unit Test,
It is very tough to start walking this road of writing tests and code to support it. Its like a new years resolution to lose weight and be healthy.Not many do it, but those who do, Do live longer:)