Jan 20, 2010
I'm trying to get into Unit testing for the obvious positives it introduces, and I'm trying to write a Unit test for a class I wrote the other day. (I know this is the opposite to TDD, please bear with me) My class, Image, is used in conjunction with some others for image manipulation. Image essentially wraps a GD image resource and stores data along with it. For example, an instance of Image will always contain it's current state, i.e. its new width/height if resized, the original image data, etc. The Image class also contains methods for,
View 3 Replies
Creating itself from a file, string data, or URL, e.g. $image->loadFromPath() Creating a new GD image resource from the properties of the current Image instance, e.g. for image resizing to maintain background transparency, etc. Cloning the GD image resource for use in the manipulation classes
What I'm struggling with is how to Unit test this class properly with PHPUnit. I've done some reading and I have a few conflicting ideas on how to approach it and I don't know what's right. Do I, Write a test for each method of the class. I read somewhere that I should test each and every method. However, some of the methods run others (rightly so may I add), so you then have a chain of dependency.
But I also read that each Unit test should be independent from the other. So what do I do if this is the case? Write each test as a usage route of the class. I also read somewhere that each test should instead represent 1 path/usage route you can take with the class. Therefore if you cover every usage, you'll ultimately get complete code coverage.