Josselin Dionisi - Freelance developer

Understanding the importance of unit and functional testing

ionicons-v5-k Josselin Dionisi Sep 27, 2022
69 reads Level:

Today, I've chosen a subject that is sometimes neglected by developers, but is nonetheless very practical: testing.

No, I'm not talking about testing your code before sending it to production by pressing F5 to see if everything works. 😛

I'm talking about automated tests that you write following the logic of an algorithm to create practical cases that can be launched at different times.

Imagine you have an e-commerce site and you want to test that a product is actually added to the shopping cart (which is essential). Well, you can write a test that will reproduce all the actions that a real user would perform to check that everything is running smoothly.

Unit tests

Unit tests act on the elementary bricks of your code, on the smallest possible scale. Their main purpose is to check that your code is "clean" and that its foundations do not contain any problems.

For example, if you send an authentication request to your API, it must return a token. This is a test that can be written quickly and will take a few milliseconds to execute. But it is undoubtedly very important for the overall functioning of your application.

So if every time you update your application, you take a few seconds to run this test, you'll be sure that this part works without having to test it manually.

It's true that you'll need a little time to write your tests, but you'll only have to do it once, and then it'll be automated, saving you time 😀

A little doc if you want to dig deeper into the subject:

Testing in PHP :

PHP Unit documentation

Testing with React :

Documentation unit tests with React

Functional testing

Here, in contrast, we're going to test your app's functionalities rather than its code. Instead of testing API authentication as in unit testing, functional testing reproduces end-user behavior. This is known as end-to-end testing.

Your test function will literally go to the login page, fill in the form fields and simulate pressing the login button.

You can then go a step further and check the text that appears on the page after your action. This will allow you to see if you have an error message or, on the contrary, a successful login.

Writing a functional test on the Symfony framework :

Functional tests in Symfony

Execution and automation

Now that you've written your tests, you can run them after each development you've carried out on your application. In just a few moments, you'll be sure that you haven't broken any important functions with your modifications.

If you remember [my article on CI/CD](https://developpeur-freelance.io/blog/ci-cd/), you'll also know that your tests can run automatically each time your Git repository is updated. 😉

Now you've got an overview of what tests are for and why they're important, the longer your project will take.