We choose to become developers to write code… but at the heart of this profession, it’s tests that really matter, as they are crucial for ensuring the quality, stability, and security of the code. With so many types of tests available, it’s easy to get lost when you’re a beginner! The goal of this article is to introduce you to the three most common functional tests: unit tests, integration tests, and E2E (End-to-End) tests.
Why test?
I have a saying in mind: “To test is to doubt” haha ^^
Hmm, as a developer, sometimes it’s good to doubt a little! Adding tests to our code is essential, as it ensures that the implemented features remain reliable over time. It’s impossible to keep everything in your head, but by adding tests, we ensure that the code works correctly in the long run.
When should you add tests?
When adding a new feature, there’s no doubt that you need to add a test to validate its proper functioning. The same applies when refactoring the code: even if some classes are removed and new ones are added, it’s crucial to ensure that everything works as expected.
There’s also another situation where adding tests is just as important! When fixing a bug, it can be wise and relevant to add a test to ensure the issue won’t recur. In fact, if a bug has slipped past the existing tests, it likely means an essential test was missing.
And be careful, because if a regression happens and an old bug resurfaces, it can seriously annoy the client and damage the trust they had in you… and it can be hard to regain that trust afterward…
Types of tests
There are many types of tests, each suited to specific use cases.
Static tests
Static tests are performed without executing the code, which allows for quick and cost-effective detection of errors such as typos, incorrect typing, etc.
Personally, these have helped me identify several bugs that slipped through my unit tests. Static tests also help eliminate code that will never be executed…
For example, you can use PHPStan for PHP.
Unit tests
As the name suggests, unit tests are used to test small portions of code, such as a function, in isolation. They are simple to implement and execute very quickly. Ideally, every branch of your code should be covered by unit tests.
For PHP, you can use PHPUnit, and for C++, Google Test. I mention these two tools because they are the ones I personally use.
Integration tests
Integration tests aim to verify the proper functioning of interactions between several components of an application, ensuring they work together correctly. Unlike unit tests, which focus on isolated pieces of code, integration tests validate how these code units fit into a larger system.
The tools used for integration tests are often the same as those for unit tests, as both aim to automate the verification of the code’s correct functioning. However, their implementation and usage may differ slightly based on the objectives of each type of test.
E2E tests
E2E (End-to-End) or functional tests go even further: they simulate user actions in a complete environment. These tests validate specific scenarios in the application, such as authentication, balance checking, or form submission.
For a web application or website, E2E tests simulate real user behavior in the browser.
For example, to test an Angular application, you can use Cypress.
And the other types of tests
Manual tests
Manual tests, while time-consuming, allow for human validation of a new feature delivery or the prevention of a potential regression.
Although you can’t test everything manually, it’s essential to focus on frequent and critical use cases of your application to ensure they work properly in a real environment.
Performance tests
Performance tests measure an application’s ability to function under a specific load, evaluating aspects like response time, stability, and scalability. These tests help identify bottlenecks and ensure the application can handle a large number of users or prolonged usage without slowing down.
Security tests
Security tests aim to identify vulnerabilities in an application that could be exploited by attackers. They include checks for common vulnerabilities such as SQL injection, XSS (Cross-Site Scripting) attacks, and input validation issues. Security tests may also include penetration testing, where attack simulators try to exploit these vulnerabilities.
Conclusion
We’ve explored the different types of tests primarily conducted by developers, though some categories require the expertise of other professionals, such as security testers. There are certainly other types of tests depending on the specific needs of each project, but those mentioned here are the most common and essential for ensuring the quality, security, and performance of applications.
Leave a Reply