The unit test is written first

The unit test is coded first before a single line of code is written.  Writing the test first is much less efficient than writing the code first.  The reason for this is that testing involves baggage.  What this means is that if some test cases exist for a component and some significant changes need to be made to that component, then the test cases will probably have to be changed as well.  This usually requires significant extra time and effort.  If the test cases have not been written yet, then there is no baggage and no changes have to be made to test cases because they don’t exist yet.

When writing the test first, the emphasis has changed from focusing on the code and coming up with the best design and implementation to meet the requirements to focusing on the tests.

This practice also encourages overly simplistic methods that lead to a poor overall design, bloated code, and makes it harder to maintain.


Prev          Next          Back To problems with agile practices topic

Copyright 2011 by Robert G. Bryan

2 Responses to The unit test is written first

  1. You miss a key benefit of writing the test cases first. Oftentimes, writing the tests first requires you to focus on the requirements and it oftentimes leads to needed clarifications of the requirements. I do admit that this result is more likely with black box tests than with white box tests.

    • Bob Bryan says:

      If the code has not been written yet, then I suppose you are right in that thinking about the test will help to clarify requirements. But, that does not mean to imply that this is the best way to go about developing software. When writing the test, is not one of the things a developer should be thinking about is – how to break the code? This is just one example of why writing a good test is quite different than design and coding. When designing and coding, it is important to not allow testing issues to distract the focus of the developer from thinking about the requirements, design, and code for that design.

      When writing new code, it is not uncommon to refactor it a few times while it is being written. If the tests have already been written, then the developer loses productivity by having to refactor the tests each time the code is refactored. If testing is put off until all the code has been written and desk checked, then this saves time and increases productivity.

      Another problem with writing the test first is that, according to some individuals, it tends to lead the developer to write overly simplistic test cases which tends to lead to poorly designed components.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s