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.
ASP.NET MVC exposes extensibility APIs that enable developers to participate in the resolution and instantiation of controllers, and which enables Dependency Injection / IoC frameworks to be cleanly integrated within this process.
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 ?
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.
So to test the model, should I include some portions of the framework in the unit tests ?
The least the better. Unit tests should require little to no dependencies. If class A
depends on B
, you should mock B
to ensure that if B
fails, it doesn't make A
fail too.
The main advantage to unit tests (when done correctly) is that it allows you to easily pinpoint the problem. If A
fails because of its B
dependency, you will first look at A
, then B
. Again, if B
depends on C
and C
fails, you will have to look into A
, B
and then C
. This pretty much ruins one of the greatest advantage to unit testing. If all tests are done properly, a failure in C
will not cause a failure anywhere else than in C
, 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:
$return = $this->addOne($some_param);
assert('$return == $some_param + 1');
By the way, there is no difference between unit testing MVC as opposed to unit testing in general.
Should unit testing be built into the framework, or we just create a new folder called tests
If you're using a third party framework, it will usually include some unit testing helpers, but you'll want to put your own test classes into a separate folder so that (for example) you can roll a release of your software for distribution that does not include them.
include all necessary classes and unit test each one ?
You'll generally have one test class per application class. So, if you have a model class M, you'll have a test class M_Test (or whatever naming convention you adopt.)
If you're not already familiar with PHPUnit
, you'll want to grab it and read their docs.
Unit testing should be part of the MVC framework. For example have a look at the Unit Testing Class chapter of the CodeIgniter user guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With