Sunday, May 26, 2013

The importance of maintaining tests

Today I read this paragraph from the Part 5 of the Django Tutorial, in the "When testing, more is better" section:

It might seem that our tests are growing out of control. At this rate there will soon be more code in our tests than in our application, and the repetition is unaesthetic, compared to the elegant conciseness of the rest of our code.

It doesn’t matter. Let them grow. For the most part, you can write a test once and then forget about it. It will continue performing its useful function as you continue to develop your program.

[...] At worst, as you continue developing, you might find that you have some tests that are now redundant. Even that’s not a problem; in testing redundancy is a good thing.

I strongly disagree with this assumption, specially because it is a text which is read by more self-taught programmers than software engineering professionals and it can be quite misleading. The code of tests is as important as production code, and therefore it should be maintained as well, at least if you want to gain the benefits of having a robust test suite. The reasoning for this statement is masterfully argued by Robert C. Martin in his book "Clean Code", and I will try to summarize it with my own words:

The change of test and production code is closely related, so you cannot expect modifying your production code without touching any test. However, a whole suite of "quick and dirty" tests could become a tangle structure that nobody wants to change. In that moment, the reliability of the test suite is gone and now it is another problem. Then you will find yourself in the situation of developing without tests: your production code becomes more rigid as it growths since you are not able to know if your broke something in your last change. Finally, the problem of a messy test suite has extended to your base code.

Of course, in a personal Django application it is not such a big deal, but don't forget that if this person becomes a member of a team to develop a bigger project, those habits should be corrected - and experience has taught me that it is easier to learn something the proper way than changing old and bad habits.

1 comment:

  1. Hi Alejandro

    Just dropped in to thank you for reviewing my to be released book on Tkinter (Packt Publisher)

    I really appreciate your taking up time to refactor and improve on a lot of code snippets that I had originally put up. I have incorporated most of your suggestions into the book.

    Just wanted to let you know that the review process was a great learning experience for me.

    thanks & regards
    Bhaskar Chaudhary

    ReplyDelete