Development Driven Testing
As developers, we lose a lot of benefits by testing after the fact.

Search for a command to run...
As developers, we lose a lot of benefits by testing after the fact.

No comments yet. Be the first to comment.
Testing is almost always difficult when developing applications that interact with external services, APIs, or complex features. One way to make testing easier is to use stub classes. Here's how I usually work with them. Quick intro on benefits Stubs...

We're upgrading from Laravel 9 to 10, and PHPUnit 9 to 10, and, if all goes well, we'll top it off by migrating to Pest, the latest version. To keep the positive vibes on I'll skip an upgrade from PHP 8.1 to 8.2, just because I don't want to restart ...

GitHub's 2000 minutes per month of free usage of GitHub Actions is usually enough to handle light workflows. However, as more tech is run automatically, we are forced to upgrade to a paid plan or optimize our workflows. Currently, I am running Rector...

A nice refactoring I've learned recently is to utilize types in conditionals. For years I’ve been doing if (!empty($posts)), but Rector, a tool to automatically refactor code, changed that to if ($posts === []). The main advantage of using $posts ===...

Laravel 10 is making the headlines these days. However, many apps are stuck in older versions, or at least still use the syntax and methods of older releases. One of the most significant changes introduced in Laravel 9 is the syntax for accessors and...

One of the first roles that I've been working on since I went full-time remote was that of the unit tester. I wasn't used to being the QA guy before and although this was new to me, I've gotten the hang of it. However, as part of my role, I had to write the tests as well (PHPUnit). The other devs in my teams were not initially comfortable writing tests, or they were just more focused on releasing stuff.
I am sure this doesn't happen only to me. I'm sure there are countless other projects out there that follow the same process. Devs do the feature, someone does some QA, and then the task card is moved to the Unit Testing column. For anyone appreciating a TDD workflow, that's just straight-out weird.
What is this Development Driven Testing thing weird? It's weird because you lose so many of the benefits of writing tests alongside the features.
For starters, let's say you found the feature to test to be very badly architected because of... deadlines. So bad that writing tests for it is either too difficult and non-productive, or the only way to do it is to refactor the whole thing. But the other dev has already delivered a refactored code, should you do it again? Or just skip the whole testing thing and release it, because, of deadlines?
Let's say the code to be tested is well-written and can be worked with. By what metric? Most of the untested code that I've seen is controllers, services, and methods with hundreds of lines of code and very high complexity. Sure, it may look simple and well written for a programmer's eye, but what does Phpmetrics have to say? Even if I, the tester, wrote a large feature without tests, I'd still produce the same suboptimal code that another programmer would call mehh 😂.
The other way around with untested code, over-engineering, is also a problem. It's easy to get distracted from a feature you're building and start accounting for details and sub-features that weren't requested and will probably never be used. It's hard sometimes to just stay focused and build only what you need to build. It's hard to know when to stop refactoring, but then again, it happens.
Ever worked on a legacy project? Why do all legacy projects have the same problems? How did they come to be like that? My biggest bet is that they grew with time, started making real money, and everyone was too afraid to mess around and "do things properly". Why upgrade and groom a project if it's working? If only there was a way to improve the project safely...
That's another big misconception when it comes to writing tests alongside features. I've written tested code and I've written untested code and I can say with full confidence that tests haven't cost me as much time as I anticipated they would. There are many cases when a testing environment helps me to write a feature faster. For example, when implementing a form with various inputs. I'm writing the inputs one by one and without tests, I have to check constantly with the browser to make sure that everything works. Otherwise, I don't even have to leave the editor.
Oh, and it's this other critical win when using tests. They change your way of thinking about problems, or rather, they help you uncover a sane path to dealing with them. The reason for this is that you are forced to think about use cases and implement them in sequential steps, which is very similar to the way machines process instructions. Eventually, you write code that follows this sequential thought process, steadily built with pieces that form a whole feature, and guess what, it will be very readable and easy to grasp by an outside observer, or future you.
I still think writing tests after the fact is here to stay. People who make decisions are not easily convinced by some software "theories", right? They want hard facts and results. However, the decision on this is in your hands, fellow developer. You're not going to change anything by asking for permission.
Funny enough, to prove this point even to myself, I got introduced to a modern Laravel project that had no tests, and I insisted that I would only accept the work if I was allowed to write tests. A few months in, we needed to do a major refactoring and upgrade to PHP 8.2. We did that, the tests were all green, and we were good to go. We all felt safe and protected and decided that every developer write their own tests, the benefits were obvious.
So give it a try at your projects. YOLO, don't tell anybody. Just write some tests for your own tasks, let them hang in there, and grow them in size for some months, you'll see the benefit in time. Your whole team will see.
With the recent rise of ChatGPT and GitHub Copilot, the experience of writing tests has gone to another level. Once you get a bit comfortable with writing stuff by hand, an AI assistant will just blow your mind. Copilot is already generating so many of my tests these days, it barely takes minutes to write complex logic and assertions. I feel like these tools were made for TDD.
I thank you for reading this far and I hope you got some value for your time. I write about refactoring and testing from time to time. See you in the next post.