Amitav Roy

Blog on web and travel

PEST the testing framework that brings simplicity and joy

Posted on 17th July, 2021 by Amitav Roy

The PEST testing framework - crafted carefully to bring simplicity to testing and bring joy. Yes, this is kind of the pitch that the framework has and I couldn’t agree more.

Use the descriptions as documentation

Tests are like documentation to the functionality of your application. External documents are difficult to maintain. It takes time and effort to keep them updated with the development. But, tests are the living documentation of your application or business logic. And, the syntax of the framework is so intuitive, that the tests look more like a story rather than code.

Tests help you to push to production with confidence. It also helps new developers to read this living document and understand about the code.

The syntax of the framework is so intuitive, that the tests look more like a story rather than code.

Below is one such example:

Sample PEST test vs PHPUnit test

The syntax allows you to write tests in a very simple manner. It is also very easy to read. Also, adding description to a test instead of long function names makes it so easy to name a test. And, it results in better readability of code. If you see the above image, you will see how PHPUnit tests have so much info. While writing PHPUnit tests, I need annotations. I have keywords like "public", "function". Even the function name broken using underscores.

But, if you look at the code below I am sure you will find it easier to read.

Zero effort to switch to PEST from PHPUnit

Question might come up, well I have a huge test suite already. I like the syntax and everything, but I can’t rewrite the entire suite. Well, let me tell you that it takes almost zero effort to move from PHPUnit to the PEST framework. You can even have some tests written in PEST while the others can continue to be in PHPUnit. Now, how cool is that?

Laravel the first class citizen

Although the framework can work with other PHP projects, its written with Laravel in mind. So, writing tests inside a Laravel application is a very smooth experience. Code for testing things like testing Events, Emails and other stuff are same as how you would do it in Laravel. This means, although it’s a new framework, the learning curve is not that high.

Higher order tests and Expectations API

The higher order tests (HOT) along with the Expectations API is a great way to work with your tests. It allows you to chain methods to get a cleaner test and write less code.

Higher order test with PEST

In the above code we have chained the expect and the toBe method to the it function. This means, even closures are not required.

I am finding this syntax to write tests very interesting and intuitive. I can already see that my tests are a lot more readable. It’s easy to write. And with the new syntax, I can use everything there is in PHPUnit without the pitfalls of the syntax. Let me know what you feel about this new testing framework. If you are a visual learner, you can watch this video as well:

You can find me on twitter @amitavroy7 or you can even discuss on my Youtube channel.

Comments

William Mandai liked on 18th September 2021
Debjit Biswas on 18th September 2021
Debjit Biswas liked on 18th September 2021
Helder Willian liked on 18th September 2021
amit liked on 18th September 2021
Ankur Khurana replied on 18th September 2021
Very well articulated @amitavroy7 . Thanks for sharing your thoughts on Pest, they are very helpful . <a class="u-mention" href="https://twitter.com/amitavroy7"></a> <a class="u-mention" href="https://twitter.com/enunomaduro"></a> <a class="u-mention" href="https://www.amitavroy.com/"></a>
Sachin Rai liked on 18th September 2021
Gaurav Makhecha liked on 18th September 2021
PEST on 18th September 2021
Nuno Maduro on 18th September 2021
Luke Downing on 18th September 2021
PHP Synopsis on 18th September 2021
WowDevQA on 18th September 2021
PEST liked on 18th September 2021
Albin Varghese liked on 18th September 2021
Nuno Maduro liked on 18th September 2021
WowDevQA liked on 18th September 2021
Luke Downing liked on 18th September 2021
नीरज Tangariya liked on 18th September 2021
Geet Purwar liked on 18th September 2021
Bhushan Gaikwad liked on 18th September 2021
Bhushan Gaikwad replied on 18th September 2021
Yes <a class="u-mention" href="https://twitter.com/amitavroy7"></a> <a class="u-mention" href="https://www.amitavroy.com/"></a>
John Kary on 26th July 2021
Kamlesh Paul 🌟 liked on 25th July 2021
Nuno Maduro liked on 25th July 2021
Ankur💻🎧💪 liked on 25th July 2021
JD Lien liked on 25th July 2021
Dhanush N liked on 25th July 2021
Md Shadab Alam liked on 25th July 2021
Ankur💻🎧💪 replied on 25th July 2021
Thanks bhai <a class="u-mention" href="https://twitter.com/amitavroy7"></a> <a class="u-mention" href="https://www.amitavroy.com/"></a>
Mohammad liked on 25th July 2021
Jorge González liked on 25th July 2021
amit liked on 25th July 2021
Zubair Mohsin liked on 25th July 2021
Yeshi liked on 25th July 2021
★ Dhruva liked on 25th July 2021
Arturs Krapans liked on 25th July 2021
PEST liked on 25th July 2021
Roman Pronskiy liked on 24th July 2021
Mark Almadin liked on 21st July 2021
Felipe Fernandes liked on 21st July 2021
Padam Shankhadev liked on 20th July 2021
Sebastian liked on 20th July 2021
Freddy on 20th July 2021
Nuno Maduro on 20th July 2021
Luke Downing on 20th July 2021
My Lnk App on 20th July 2021
Mahmoud Habsh liked on 20th July 2021
Nuno Maduro liked on 20th July 2021
My Lnk App liked on 20th July 2021
Luke Downing liked on 20th July 2021
Freddy liked on 20th July 2021
Ishan Vyas liked on 20th July 2021