Test-Driven Development (TDD) is a development paradigm where tests are written before the code. It may seem like putting the cart before the horse, but the idea is that tests are written to test the design first and then test the code according to the design.
The main hurdle with TDD is that it takes longer to create useable code, but that code will be more stable, easier to integrate, maintain, and update with new features. Even though the initial development takes longer, over the lifespan of the code this is the more efficient approach.
TDD forces the developer to walk through the logic before writing a line of code, which clarifies ideas and provides a better overall understanding of the feature to be implemented. Also, it forces the developer to organize the code to simplify testing, reduce the number of embedded loops and ifs, and consolidate and generalize the code (avoid writing similar code multiple times).
A major part of writing tests involves designing mock test data. Multiple tests will use the same data or a slight variation of it. The test data should be unified, parameterized, and should come from a single source. A comprehensive version of objects to be mocked should be built, even if not all fields will be useful at this stage. The mocked data represents the state for different processing points, so special attention should be payed to the consistency of the data. Mocked data may be used in two ways: as input and to verify the output. This allows for tests to be split into units that work between different execution points in the code.
Small functions should have their dedicated tests, which simplifies testing and simplifies the review of cases and code branches.
These are some ideas to take into consideration during the design and development of tests in order to create a logical and robust testing infrastructure for your project. Lastly, if you have any questions around the advantages and design considerations of Test-Driven Development, please don’t hesitate to reach out to us at any time. We’d love to help you get started.