Amitav Roy

Blog on web and travel

PEST the testing framework that brings simplicity and joy

Posted on 17 Jul 2021 by Amitav Roy

PEST the testing framework that brings simplicity and joy

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:

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.

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.