Unit testing with TDD

What is the difference in the way we write unit tests in the context of TDD? The major differentiator is in when. While traditionally unit tests are written after the implementation code is done, in TDD we write tests before—the order of things is inverted. Without TDD, the purpose of unit tests is to validate an existing code. TDD teaches us that unit tests should drive our development and design. They should define the behavior of the smallest possible unit. They are micro-requirements pending development. A test tells you what to do next and when you're done doing it. Depending on the type of tests (unit, functional, integration, and so on), the scope of what should be done next differs. In the case of TDD with unit tests, this scope is the smallest possible, meaning a method or, more often, a part of it. Moreover, with TDD driven by unit tests, we are forced to comply to some design principles, such as keep it simple, stupid (KISS). By writing simple tests with a very small scope, the implementation of those tests tends to be simple as well. By forcing tests not to use external dependencies, we are forcing the implementation code to have a separation of concerns that is well-designed. There are many other examples of how TDD helps us to write better code. Those same benefits cannot be accomplished with unit testing alone. Without TDD, unit tests are forced to work with an existing code and have no influence on the design.

To summarize, the main goal of unit testing without TDD is the validation of the existing code. Unit testing written in advance using the TDD procedure has the main objective specification and design, with validation being a side product. This side product is often of a higher quality than when tests are written after the implementation.

TDD forces us to think through our requirements and design, write clean code that works, create executable requirements, and refactor safely and often. On top of all that, we end up with high test code coverage that is used to regression-test all our code whenever some change is introduced. Unit testing without TDD gives us only tests and, often, with doubtful quality.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.222