I’m new to unit testing in general, but wanted to implement it in MVC pattern (using PHP). Now I’m not sure how to approach this.
Should unit testing be built into the framework, or we just create a new folder called tests and include all necessary classes and unit test each one ?
In short, if there is a model M, it also has some coupling with the framework itself. So to test the model, should I include some portions of the framework in the unit tests ?
Are there some good code examples on how to go about accomplishing this.
You should definitely create a separate folder for it. Cluttering up production code with tests is generally not a good idea for performance and debugging reasons.
The least the better. Unit tests should require little to no dependencies. If class
Adepends onB, you should mockBto ensure that ifBfails, it doesn’t makeAfail too.The main advantage to unit tests (when done correctly) is that it allows you to easily pinpoint the problem. If
Afails because of itsBdependency, you will first look atA, thenB. Again, ifBdepends onCandCfails, you will have to look intoA,Band thenC. This pretty much ruins one of the greatest advantage to unit testing. If all tests are done properly, a failure inCwill not cause a failure anywhere else than inC, so you will have a single class to lookup to fix the problem.To really make your code error-proof, you can use unit tests in conjunction with PHP assertions:
By the way, there is no difference between unit testing MVC as opposed to unit testing in general.