Without changing anything else, your functional tests can improve a lot if you just introduce element of randomness. You will find them to be much more reliable and robust. Sometimes even find errors you couldn’t find otherwise.
But let’s start from the beginning…
What Are Functional Tests?
Functional tests are testing entire parts of your system. Unit tests, in comparison, might only test a single function.
Functional tests could be testing entire API endpoint. For one set of inputs, they test if the correct set of outputs was provided.
For example, for this user ID and token, functional tests are checking if the correct user and data was returned.
Hardcoded Test Values
Your tests are very useful even with hardcoded values. In case of an API endpoint test, let’s say you have an endpoint that updates user info. Specifically, user email.
In your test you could try to update your user email and see if the email was actually changed. For example, like this:
const requestBody = {
email: john@example.com
};
You run the tests and everything works ok.
This means ALL the individual parts (all the functions, data retrieval, database storing, etc.) – everything was working properly.
Otherwise this new value wouldn’t end up in database correctly.
And in general, functional tests give you the biggest testing ROI. Because you cover the biggest parts of your system and more or less confirm that all the individual parts are working ok.
Of course for system-critical software you definitely need unit tests. Plus, much more testing in general.
But for your regular web apps, having functional tests removes 90% of your headaches using only 10% of the effort.
Randomized Test Values
However, having randomized values in your automated functional tests is way better than just using hardcoded values.
Why?
Because it can discover errors in your code that would otherwise go unnoticed.
Let’s stick to the example with API that is updating user email address (that we had before). Below is an example of using chance.js library:
const requestBody = {
email: chance.email()
};
You can see that the code is practically the same, so it’s not like you have to put any more effort. And you will still end up with better tested app.
But this test will now fail.
Why?
Because it turns out, somewhere in your code, you had an email validator. But this email validator only worked properly for “.com” mail addresses.
And in your hardcoded example you had “.com” email (john@example.com).
So this flawed email address validator would never be discovered. Maybe only if your users started complaining.
So when this library started throwing wide variety of (valid) email addresses, the issue in your code was quickly discovered.
And this is why having randomized values in your functional tests is such a powerful thing.
Because it will make your code better and more robust. And you will appear to be as more competent and professional developer.
Maybe you even get paid more.
P.S. -> read this article if you want to double your salary