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% [?]
Building a better mousetrap
September 7, 2004
My first project that I plan to release, DynamicImage, isn’t groundbreaking or revolutionary. There are plenty of other components out there that do the same thing. However, I’m using this project, which is relatively small, to fine-tune my development processes and habits. I believe that incorporating as many of these tools as possible ultimately leads to a better development experience as well as a better product. You can find out more about the tools by following the links or by Googling on “test driven development” or “continuous integration”.
- NUnit
- NUnitASP
- FxCop
- NCover
- NAnt
- NDoc
- CruiseControl.NET
- Draco.NET
- Log4NET
- Any of the Microsoft Application Blocks
I wish I could say that I am using them all and that they are deeply embedded in my processes. Unfortunately, that is not true. But I am making progress. I am using NUnit to test my DynamicImage control as I develop.
It’s working out better than I expected. I’ve got about 50 tests in a abstract test class which is implemented by landscape, portrait and square image test classes. So with one click I can run 150 test cases on the control. So far this is just for the Resize method. It’s been great being able to make a change and validate that nothing broke so quickly. The logic in resize isn’t that complicated but a couple of tweaks I am adding might work with one image type but not another and NUnit quickly works through all of that for me.
I’m hoping that I will be able to use NUnitASP to test the HttpHandler portion the control. After that is probably NDoc. Once I get those done, then I will be ready to release my first version.
Popularity: 4% [?]
DynamicImage
September 1, 2004
In my initial post for the goals and direction for this site, I mentioned that I wanted this site to be a place for me to publish and release the various software projects that I am working on. The first project that I am working on is DynamicImage, a handler to receive incoming image requests and a processor that will manipulate and render the image appropriately.
While this particular application isn’t groundbreaking or terribly exciting, it is an exercise for me in polishing, wrapping up and releasing a piece of code. I’ve been using a similar project for at least 2 years on my personal server to serve up resized images for a photo album. I am in the process of cleaning up my code and adding some additional features. I’ve made attempts in the past but haven’t gone too far. Hopefully with this site I will have the encouragement and motivation to move forward rather quickly.
Popularity: 5% [?]