During the last months, I started paying more attention to unit testing and writing tests in my symfony apps. But not until this awesome post from Bernhard Schussek, I really understood how you can make your testing life easier and get tests done better, faster and be more efficient.
It also helped me to understand, to which granularity you can and should break down your tests as well as your methods in the lib classes.
So this really is a BIG FAT recommendation on reading the article!!
What I learned from it and what I am doing ad enjoying right now, when creating new functionality:
- If I have a complex method to write, I break it down into the smaller parts that need to be done step by step. For this, I write the logic with methods that are not even exisiting yet.
- Next, I write the unit TESTS for these non-exisiting methods, filled with test cases that make sense in the current context of the problem and help to make sure the method works
- Next, create an empty stub of the new method, so you can start running the test without a crash – but of course it will fail
- Add functionality to the method to make sure you finally pass all tests.
This really makes the whole process a lot of fun because it really is rewarding to see the “All tests successful.” or “# Looks like everything went fine.” at the end.
And you can continue with your next part of the more complex functions with a good feeling of confidence, that in the end it will all work fine 😀