Let’s all agree on a common definition of unit testing to start with and quote the esteemed Martin Fowler in one of his blog articles dedicated to unit testing, in which he states having encountered no less than 24 definitions of unit testing 😱
In the end, this is the definition which he believes to be most accurate, and we would tend to agree:
“Despite the variations, there are some common elements. Firstly there is a notion that unit tests are low-level, focusing on a small part of the software system. Secondly, unit tests are usually written these days by the programmers themselves using their regular tools - the only difference being the use of some sort of unit testing framework. Thirdly, unit tests are expected to be significantly faster than other kinds of tests.”
It’s testing the smallest entity in your code in order to significantly improve the quality of your code, avoid regressions and provide documentation.
Clean and bug free code means performant software and happy users! Plus, you will feel more confident about your coding and will be even bolder and more creative regarding your future development projects, ain’t that nice 😏
If you want to know more about the benefits of unit testing, we can only encourage you to check our previous articles.
As previously said, unit testing is only about testing the smallest entity of your code so let’s take a function to perform the test, here : isEmailValid.
You could test in the same file but the best practice is to create a separate test file for better clarity for your colleagues and your future self 😉
Then, we will need to import the function. Once it’s done you may write your test for the function is email valid as below.
You want to make sure that edge cases are covered in your tests, so here are some examples of email addresses that wouldn’t do and consequently wouldn’t pass the tests.
It’s time to run your tests and if you have followed the previous steps you should get the same result as shown here below:
First you’ll need to install the Ponicode VS Code extension on the marketplace. Once it’s done link it and copy the token appearing at the bottom right of your screen right here ⬇️
Open the file you’d wish to test. On the left of your functions, you will start seeing little ponies, that means that the tool is able to make suggestions for these. Right click on the function you want to test and select “Ponicode Unit Test”:
A window will appear on the right of your screen showing the suggestions our AI-powered unicorn assumes are relevant to test; choose the inputs you would like to test and press the little thunderbolt to run them. It only takes a few seconds to run and see if they pass or not!
Once you’re done, you can find all your tests in the automatically generated test file which should look like this:
Voilà! Now you know everything, you can go practice on your first unit tests 🎉
Of course if you are curious about our AI-powered solution for unit testing, you will find plenty of videos on our YouTube channel but if you’re in a hurry, here is a 1-min demo of the steps we’ve detailed below, enjoy and write beautiful code!