Software Testing And The Importance Thereof
July 18, 2022Rules For UX Designers
August 8, 2022When it comes to building software, software developers and the business they support want it to go as quickly as they can and at the same time, they want the code to work without any defects or bugs.
These needs require focus on two things: Speed and Quality.
People think of quality vs speed as a dichotomy, you can have quality and it will take longer or you can have speed but there will be defects, but you can’t have both. But when it comes to Test Driven Development the quote “The only way to go fast, is to go well.” by Robert Martin is well applied.
Test-driven development decreases defects, it makes developers go faster and produces better code.
Before we talk about Test Driven Development let's talk about automated testing.
Automated testing is about tests that can automatically be repeated. It is code that verifies other code. The test executes the code that is being tested and then uses an assertion to make sure the code performs as expected. These tests are referred to as the test suite and define what the production code should do. Tests are not shipped with the software they are only used by developers and those involved when building the software, the code that they test though does get shipped to the customer and is called the production code.
So what is Test Driven Development?
Test Driven Development (TDD) is a process formed by repetition of a short development cycle. Test Driven Development is referred to as the Red, Green, Refactor cycle. You begin by choosing a small task, the next step is to write a failing test referred to as the Red test. In step three you write the simplest code to make the test pass, this will be the Green test. Step four is refactor, and that completes one run of the TDD cycle. Step five is to repeat the cycle.
The difference between Test Driven Development and Unit Testing.
TDD is a process, it can be used with many categories of test including unit test but also with others like integration test and acceptance test. Unit testing focuses on just one type of test, the unit test, TDD is the approach of writing test. The second thing that distinguishes TDD from unit testing is the order. Test Driven Development as the name implies drives the code, unit test on the other hand is often described as test-last or test-first approach. During a test-last approach the production code is written first and then the tests are added. Test-first approach often involves writing up to several tests upfront and then writing many lines of production code.
The second distinction of TDD is that it differs from test-last and test-first approaches, because the developer writes one test and then minimal code to make the test pass, thus the test shapes the implementation. Although the difference is subtle it is powerful since it does not matter about what programming language the developer uses or problem domain you operate in. TDD is a skill of thinking about the problem you want to solve before you want to solve it. And if developers take time to get clearer about the precise problem they are facing, they will be better able to solve it.
Another definition to cover in TDD is code coverage. Code coverage is a measurement, a percentage of production code that is executed by automated tests. For example, if there are 1000 lines of production code and 800 lines executed by tests then 80 percent of the code is the code coverage.
Measuring code coverage is separate from the way tests are written, therefore code coverage can be used regardless of if whether a test-first/test-after or TDD process are used.
It is however important to note that all new code has almost a hundred percent code coverage when TDD is used.
Two key takeaways from Test Driven Development:
- TDD increases code quality and makes it much easier to add and test new functionality in the latter stages of development.
- TDD impacts productivity in that productivity decreases in the short term but increases in the long term.
Below are Frameworks for Test Driven Development based on different programming languages: