Assembly testing DynamicImage
September 9, 2004
I’ve really enjoyed the freedom that working with NUnit during the development of my DynamicImage component has given. Granted, I’ve only got one method so far (Resize()) but that method has close to 150 test cases against it. The code for the method is maybe 60-80 lines, but the code for the unit tests for that method are in the neighborhood of 700-800 lines. The time spent on coding the Resize method is probably about a quarter of the time that I’ve spent getting the unit tests coded. Slow, but well worth it. There is no other way to quickly and accurately run that many tests manually. Several times I put what I thought was quick and easy code into place only to have it bomb on some of the different image orientations.
Testing the actual class that does the image manipulation is one thing. Testing the combination of http handler to image manipulator is another. I mentioned in an earlier post about how I’ve had the functionality for DynamicImage (but called it ImageHandler) coded for a year or two but for this project I am reworking the code to work better, be more flexible, do more, etc. One of the obstacles that I ran into on the original project was with unit testing. The problem was that all of my code for image manipulation was in my handler’s ProcessRequest() method. It was quick and convenient to put it there at the time. The problem with unit testing was that ProcessRequest() requires a HttpContext object. I spent a fair amount of time trying to create a valid HttpContext object in code and researching mock objects (which are farther along now than last summer) but was unsuccessful. Part of my goal for DynamicImage was to break the different pieces apart and architect it to where I code do proper testing.
So now that unit testing is working for the image processor, I need to be able to test the handler. But I can’t really “unit” test it since I run into the same problem with the Context object. At my day job, I have experimented with NUnitASP and it works great for testing ASPX pages. I thought that I might be able to modify the image tester control to stream the images to create an Image object which I could check Width and Height properties. However, that’s more of a “product” test scenario and I was having some trouble ’seeing’ how it would actually work. Back to the drawing board.
After a little more thought and research, I have come up with the following solution to “assembly” test the image handler. I still use NUnit (not NUnitASP) and make calls directly to my handler (DynamicImage.ashx) passing in the property values on the querystring. In my test class, have a method to load an Image from a stream that is obtained by accessing a url. I will then be able to compare the expected size with the actual size of the image returned from the handler. Not technically unit testing, but more of testing how two assemblies work together. Back it works. Of course, all of this works find for just resizing an image and checking the dimensions but won’t really help out to much farther down the road when I add features like caching and watermarking.
Popularity: 6% [?]
Comments
Got something to say?