At some point in your career you’ve been told to start writing automated tests and you didn’t really understand why? After all, var_dump was your best friend to check variables and you’re really good (or so you think) at testing everything manually. Also, these tests are more code you have to write, to maintain and to justify to your Manager. Besides, automated tests seems so complicated. All of these libraries, mocks, stubs, etc . Arghhh
So it’s a bit overwhelming to start writing tests and I’ll be trying to explain why you need to start now and how to get started and hopefully by the end of this series it will make much more sense for you. Also, I’m going to simplify as much as possible, as this was a problem I struggled with when I started programming, I will be talking to my past self, I hope it won’t get weird though 🤗
Let’s takle the big elephant in the room. I’m pretty sure by now that you’ve felt some pain when reading other people code or even your own code written a few days ago and nothing made sense to you. Well, it turns out that tests are a great and simple way to document and reason about code. This is very important as you’ll be spending most of your time reading code. Now checking that your code works manually is fine when you have a small one-time PHP script, but eventually that script grows and becomes harder to test everything and then this new guy comes up with a new idea and you have to make big changes and everything breaks, and you’re pulling in an all nighter for the fourth time in a row and you and everyone around you are just sad and frustrated. Well, automated tests helps you avoid this situation, if you had tests, you would know when things break or even catch those bugs before even introducing them. Also you can safely refactor and make big changes. Having automated tests will give you confidence, safety and respect from your peers and managers because you got your shit together.
Yes it’s, but so does everything else worth doing. Bare with me here for a while and maybe this time, you get it or maybe not but defintely you’ll learn a thing or two along the way.
Before thinking about automated tests, let’s see the most used types and see how each one is useful.
In this kind of tests, you want to isolate each part of your system into smaller parts a.k.a units and you try to make sure each unit works correctly. One additional benefit to the ones I’ve mentioned earlier is that with unit tests you’ll start thinking more deeply about your design and public API.
While in unit tests you’re making sure every part of the system works as expected, in integration tests, you group those units that represents a system or module and make sure they all play nice when put together.
You need this kind of tests to avoid this situation:
With unit and integration tests, you’re basically making sure the thing you’ve built works, but does it the requirement of what that thing is supposed to do? You’re basically trying to make sure the business and technical contract are met.
With all of this out of the way, let’s get into the fun part where we actually get to write tests. In the next post, we will get started with some unit testing and PHPUnit.