DEV Community

Cover image for Test-driven Development or TDD
Volodymyr
Volodymyr

Posted on

Test-driven Development or TDD

Let's talk about testing.

At which stage is it best to write tests?

Generally, there are three approaches:

  • Tests are written after the code

  • Tests are written alongside the code

  • Tests are written before the code

In some situations, there aren't many options. For example, in system testing, tests mimic user behavior and perform actions in a browser.

Such tests are written after the code.

For lower-level tests, integration tests, and unit tests, one of the above options is usually available. The "write tests after the code" approach is one of the least user-friendly methods.

Why?

The process of writing code itself implies constant running of the code and checking that it works. For the simplest tasks, like educational ones, this running happens quite quickly.

It's easy to execute and easy to verify the correctness of the results.

For example:

capitalize('hello'); // Hello
Enter fullscreen mode Exit fullscreen mode

In real code, preparing data to test the code's functionality can take a lot of time, minutes, or even tens of minutes.

On the other hand, the result of the code being tested can be something complex, like a large number of records in a database or the output of a particular complex structure.

Then each code run for testing becomes an entire adventure.

It's difficult to prepare the data. It's difficult to check the work's result.

For example: Loading goods from 1C into a database.

loadGoodsFrom1c();
Enter fullscreen mode Exit fullscreen mode

This is where the "write tests before code" option comes into play. For many beginner developers, this phrase confuses. How can you write tests before the code is written? It turns out, you can and it's even pleasant.

Suppose we want to write a function that can repeat a given string a specified number of times:

repeat('$', 3); // $$$
Enter fullscreen mode Exit fullscreen mode

We know what it takes as input, we know what its output should be.

Can we write tests now?

Of course!

test('repeat', () => expect(repeat('$', 3)).toBe('$$$') )
Enter fullscreen mode Exit fullscreen mode

How long does it take for an experienced developer to write such a test? I think about 15 seconds, which is exactly how long it took me to write the code above. But now, to check the functionality of this code, all you need to do is type the command jest in the console.

Testing before writing code has another powerful advantage. It forces the programmer to first think about the design of their solution, and how it will be used, rather than how beautifully they can implement it internally.

Well-designed interfaces are the key to success.

In the development world, the approach where tests are written before the code is called Test-Driven Development (TDD).

TDD
Nowadays, everyone continues to talk about this method. In it, tests are written for all parts of the code with maximum detail. This type of TDD, while emphasizing the importance of design, focuses on specific functions and classes of the application, instead of the whole picture.

But there is also another TDD, where tests on the internal parts are almost never written.

Top comments (0)