FIRST Principles and Test Smells 🚀 | DevsDay.ru

IT-блоги FIRST Principles and Test Smells 🚀

dev.to 4 мая 2024 г. Ágatha


When it comes to writing high-quality unit tests, there are some basic principles, grouped under the acronym FIRST, that can be quite helpful.

Fast ⚡

Unit tests should always be fast. The idea is that you can run them all the time, without having to wait. If you need a coffee while your tests run, they probably aren't fast enough.

Independent 🧍

Each unit test should be able to stand on its own. It doesn't matter in what order you decide to run them; they should not depend on each other.

Repeatable 🔁

If you run the same test multiple times, the outcome should always be the same: either the test always passes or always fails. Tests that sometimes pass and sometimes fail without any apparent reason are called "flaky" or erratic.

Self-checking ✅

Once the test execution is complete, the result should be self-explanatory (passed/failed). This way, the outcome can be quickly verified and interpreted.

Timely 🕰️

It's a good idea to write your unit tests as early as possible in your development process. Many people even prefer to write the tests before the code they will test; this helps ensure that your code will do exactly what you expect from the start.

Test Smells 🐟👃

Some signs in your test code may indicate that something could be improved. These signs are called "Test Smells":

Obscure Test 🌫️🤔

In code, an "Obscure Test" is complicated, long, and difficult to understand. Tests should be simple and straightforward so that anyone can understand what is being tested and why. It is important to have a clear and easy-to-understand logic.

Test with Conditional Logic ➰❓

A test with many "ifs" and "elses" makes it difficult to understand exactly what is being tested and under what conditions. Ideally, unit tests should be linear; conditional logic in tests hampers understanding.

Code Duplication in Tests 📄🚫

If you have repeated code in several tests, there might be a more efficient way to organize this.

Test Smells are like a signal that maybe you can simplify or better organize your tests. It's not that you can never have an "if" in a test or a slightly longer test, but it's a reminder to stop and think: "Is there a simpler way to do this?”

Most of this content came from the book "Modern Software Engineering", written by Marco Tulio Valente. This book is a treasure trove of tips on how to create tests the right way, using the latest and most efficient in the programming world. However, the only difference is that the book is in Java and here I adapted it to use JavaScript.

Источник: dev.to

Наш сайт является информационным посредником. Сообщить о нарушении авторских прав.

testing javascript webdev programming