There must be another cure against legacy code than writing tests … Sounds like “There must be another cure against chaos than tidying up”.
Yet for we all know the washing machine, the dishwasher, the hoover robot but not the code cleaner. Why?
I think that many developers justify this with the fact that code is to complex to clean it automatically. Actually complexity is the main impediment for automating – automating needs simple stupid tasks.
However – test writing is a kind of simple stupid:
- I choose a method to test
- I determine the state before this method that could influence the method (pre-state)
- I determine the state after this method that was written by the method (post-state)
- I combine the call of the method with different pre-/post-state pairs, setting up the pre-state and checking the post-state after calling the method
It’s yet not easy:
- the programming language can inhibit you (e.g. information hiding can deny the access to the full state you need)
- the mere size of the pre-/post-states is often too large to analyze (legacy code in particular tends to depend on large states)
Both of these problems are a typical human developer problem. Scaling and getting access to internal state is simple to an automatic tool. I would have guessed that such a tool already exists. The simple search for a tool generating or recording tests, or at least for a tool searializing program state brought no results. So I decided to create my own prototype.
My solution for the automatic test generator is testrecorder. I will post my progress on this subject in this blog.