We All Write Tests I Just Happen To Use PHPUnit/PEST

Alfred Nutile
3 min readOct 4, 2023

--

image by: Clem Onojeghuo

I cannot write most code easily or well without writing a test.

Many times, companies, managers, and even developers are resistant or hesitant about writing tests, as if they will cause a task to take more time. For years, I have been thinking about how that is “not true”. But then I realized a bigger point: we all spend time testing the code we write, just some of us use PHPUnit and/or PEST, while others use other methods like the ones mentioned below.

Reloading the browser:

Setting aside JavaScript testing for a moment, I have seen and used this method years ago, reloading a browser to make sure a Controller is returning the expected results.

Running a command:

Some people create an Artisan command that will run the logic to “test” if it is working.

Running code in Tinker or TinkerWell:

I have also seen this one. Using Tinker or other tools to run the code as needed.

Controllers that run tests:

Controllers that perform tasks like putting jobs on queues or sending emails to prove that those classes work.

Clicking in the browser to recreate user interactions and test the work:

This is the classic method where you click through the areas impacted by your work to “register for a trip” or “add a new tournament,” etc.

All of these methods are “fine,” but two things are worth noting: they all take time, and they are not suitable for everyone. Personally, I do not do TDD (Test-Driven Development) at all or even close. All my client projects range from 60–70% coverage, but my confidence level is 90%, and that is what matters. Moreover, the same time spent writing tests has several bonus effects.

First of all, I have regression tests built in, so as new features are added, I know I did not break anything prior to them.

Secondly, I have documentation of how the code and application work, so any new developer can dig around.

Lastly, and most importantly to me, this is how I write code. I start with a test and then write the code. For example, I might add a model, factory, and migration, and my Model test will ensure that the factories are working correctly. Or I might add a controller and then quickly test it using “AssertInertia” to make sure that the basics are working. I might also create a Job and then test all the logic in it. Additionally, there are API-related libraries that I use to test my app’s interactions with them via mocks and facades, which would be almost impossible without spending additional time waiting for responses.

The only area that takes more time is coming into an existing application that has no tests and writing factories as you go. However, I believe that the math is in favor of testing rather than continuing in what is honestly a dead end. You will spend more time and money on manual QA, bugs, complexity in deployment, and on-boarding new developers.

My overall point is that for those of us who have to write tests to write our code, I now see that it is not that it takes more time than the other ways; that is just a mistake in how we perceive code being written.”

--

--

Alfred Nutile

LaraLlama.io Open-Source Product Created Day to day developer using Laravel, PHP, Vue.js, Inertia