Portfolio

Blog

My articles

Test Driven Development (TDD):

Test Driven Development (TDD) is an approach to software development where writing automated tests is an integral part of the programming process. The TDD process consists of three main steps:

  1. Write a test: At the beginning, a unit test is defined to check a specific functionality of the system. Initially, this test fails because the corresponding functionality has not been implemented yet.

  2. Write code: The programmer then implements the code to meet the conditions of the unit test. The goal is to make the test pass successfully.

  3. Run tests: After writing the code, all automated tests are run to verify if the new implementation works as expected. If the tests pass, the code is ready for further development. Otherwise, the programmer adjusts the code to meet the test conditions.

TDD helps maintain code quality by continuous testing, facilitating the rapid detection and correction of errors. Moreover, tests serve as a form of documentation, describing the expected behavior of the code.

In the context of unit testing, terms such as Stub, Spy, and Mock are often used:

  1. Stub: A simple implementation of an object or function that returns a constant response. Stubs are used when isolating the tested code from other dependencies that may be difficult to control or are not yet available. Stubs return predefined values, eliminating the need to use actual implementations.

  2. Spy: An object that collects information about how it is used. It is used to monitor calls to functions or methods and gather data about the arguments passed to these functions. A Spy allows checking whether specific functions have been called as expected.

  3. Mock: An object that is programmatically configured to simulate the behavior of other objects. Mocks are more advanced than stubs, as they can be configured to specify what values should be returned, what exceptions should be thrown, and which methods should be called. Mocks are used to test interactions between different objects in isolation.

In summary, applying the TDD methodology and using tools such as Stubs, Spies, and Mocks helps developers create more reliable, modular, and maintainable software.

Web Developer

© Michał Pieróg. All Rights Reserved.

Scroll to Top