Test Driven Development. How to make efficient tests?

Test Driven Development (TDD) is a type or practice of development that consists of first writing the test and then the source code, which must successfully pass that test. Therefore, the project is developed based on the requirements established in the test. With this a more robust, secure and maintainable code is achieved, as well as a faster development.

Work process

The process would be as follows:

  1. Requirements and acceptance criteria are determined.
    For example, if a client asks us to develop a calculator that adds numbers, the acceptance criterion would be: if two numbers are entered into the calculator and the addition operation is chosen, the calculator displays the result of the operation on the screen.
  2. Unit tests are defined for all cases in which the functionality may fail.
    Based on the previous criteria, we must think that we are going to need a function called “sum”, which receives two parameters of a numerical type. We must think of all the possible cases in which it can fail: if the appropriate parameters are not passed, check that the operation is commutative, etc.
  3. A test is defined in which the functionality successfully passes the test.
    We write a very similar code to the previous one in which two numbers are correctly added.
  4. The code that makes the test pass is written.
    Here we create, for example, the calculator class with a sum method that returns a whole or decimal number and to which two parameters are also passed of type whole or decimal. Inside the sum would be performed and the resulting number would be returned.
  5. All the automated tests are executed, verifying that the ones that have to pass actually pass and the ones that have to fail actually fail.
  6. The code is refactored and cleaned, removing elements that are not necessary.
  7. All tests are run again to check that everything still works after the code cleanup.
Test Driven

Manual or Automated tests

Within this development methodology, we can distinguish between two types of tests:

Manual tests are those carried out by people who navigate and interact with the software. It requires the person in charge to set up an environment for testing and to have a good understanding of the software. These tests are exposed to human errors.
Automated tests run a code to verify that features work properly and return the expected results. They are faster and more reliable than manual ones, but they depend on your code being well developed.

Automated test types

There are many types of tests to apply TDD. Here we will name just a few:

Unit tests which I will talk about in more detail in another article, consist of individually testing functions, methods, classes, components, etc.; that are used in the project.
Integration tests verify that the different modules or services work in harmony when they work together, interacting with each other.
Property tests are those that validate properties of the code.
Functional tests verify the output of an action, its result, without paying attention to the intermediate states while the execution is carried out.
End-to-end tests replicate user behavior and verify that the flows followed by the user work as expected. It can be things as simple as logging in or loading a page, like checking email notifications or online payments..
Regression testing checks a set of scenarios that worked correctly in the past to ensure that they continue running that way. A failure in one of these tests means that a new functionality has affected another operation that was working fine previously.
Smoke testing test the basic and general functionality of an application ensuring that the most important features are executed as expected.
Acceptance testing verifies that a system meets business requirements. They are usually carried out when the development phase has finished.
Performance testing they verify how the system responds when it is under a high load. They are used, for example, to observe the response times when a high number of requests are executed and to see the reliability and stability of the system in the face of a significant amount of data.
Photo María Marín
María Marín
Full-stack developer at Virtusway

Discover how we can help you

Please leave us your question and one of our assistants will contact you asap.