Unit testing is the testing the of individual code blocks at a granular level. Therefore tools to aid this process make it very easy to isolate these blocks of interest and make sure they’re kosher independently from the rest of your application code. “Test Driver Development” is a methadology where you code tests before you even write code. For new developers--that these tutorials are written for--it’s really hard to get why it’s so important since you usually want to get to coding your features immediately. That’s why I won’t go too deep into it yet, but I will say that for large teams and apps already in production iterating into post-launch phases it’s very important. The reason is because you can run tests that will verify past code you’ve written still works. As you improve upon your app and add new features, it’s inevitable that you will break old code. With a proper suite of tests for your most important features, you can quickly generate a report that lets you know if anything broke.
The way unit testing works is generally that you define sets of input, then the test passes the input to a block of code, waits for the output, and then tests that the output equals what you want it to equal. You have to write code to setup what the input is, and you have to write code to test the values of the output and if they match your expectations.
Yii offers built-in testing tools (based on PHPUnit). To install them and learn how to use them, read these tutorials on the Yii site:
With this birds-eye-view overview you should know when this will become important. To me, the biggest thing is the mindset that unit testing cultivates. It helps you imagine your code in parts, rather than features. It lets you visualize the motor, the steering wheel, etc, if we were to use a car metaphor. It helps you pin-point how each part should interface with other parts so that you can get a rock solid interface between sets of code so that less breakdowns happen. It helps you know where the problematic parts will be, i.e. what points of interface may be weak or strong.
In another article I’ll cover Functional Testing, but not in this series. However, I’d like to point out the difference: Unit Testing is all about automating the testing of blocks of code, methods basically, while Functional Testing is all about automatically simulating a user on your site and catching any issues at the graphical user interface level. In my experience, doing Functional Testing is a lot more difficult to pull of and without the gains of Unit Testing. Functional testing is very appealing to non-technical product guys that don’t trust their developers--at least the overall concept is. Functional testing is great when your app is truly done, and you want to keep it that way as you add new features. It’s not as useful in the development stage and just getting to your initial launch, for which these tutorials are mainly written. It’s hard to prepare useful functional tests when your app barely works. Whereas unit testing--or especially test driven development where you write the tests first--is key to giving developers a mindset where they visualize their code in chunks. Overall, unit testing allows developers to really focus on their tasks and guarantee that what they’ve been assigned to do is as closed to perfect as possible. It requires great product specs, as do most aspects of software development for GUI heavy apps, so that you can also create great tech specs where you pin-point precise pockets of code to write. When a project can be broken down that granularly, unit testing does wonders by helping developers focus on and insure their assignments will interface with the assignments of other developers perfectly, or at least in the way the other developers expected.