C# – How should I unit test a bitmap modifying method

cresourcesunit testing

My Sprite class has a method:

private void updateWithBitmap(Bitmap b, int x, int y)

This will 'lay on top' the bitmap that's passed in, the top left corner of the passed in bitmap being at the x,y position of the parent bitmap.

Now I want write unit tests to check that this method is working correctly.

For example, we'll need to check that an exception is thrown when the passed in bitmap is larger, or is out of bounds of the parent bitmap.

    [TestMethod]
    [ExpectedException(typeof(System.ArgumentOutOfRangeException))]
    public void TestUpdateWithBitMap_SmallerParent_ThrowsOutOfRangeException()
    {
       Sprite parent =  new Sprite(new Bitmap(10,10));
       Bitmap child = new Bitmap(20, 20); 
       parent.updateWithBitmap(child,0,0)


    }

    [TestMethod]
    [ExpectedException(typeof(System.ArgumentOutOfRangeException))]
    public void TestUpdateWithBitMap_PassedBitMapOutOfBounds_ThrowsOutOfRangeException()
    {
        Sprite parent =  new Sprite(new Bitmap(10,10));
        Bitmap child = new Bitmap(3, 3); 
        parent.updateWithBitmap(child, 9,9)


    }

Good so far.

But also we want to test that the method genuinely is updating correctly.

enter image description here

For example, if the left most image is our parent bitmap, and the middle is the child bitmap, and we call our method with:

parent.updateWithBitmap(child, 3,3);

we get the rightmost bitmap as a result.

The question is:

Is it appropriate to store these bitmaps as resources, and then check the results are equal?

eg.

    [TestMethod]        
    public void TestUpdateWithBitMap_UpdatesCorrectly()
    {
        Sprite parent =  new Sprite(Properties.Resources.TestBitmapParent1);
        Bitmap child = Properties.Resources.TestBitmapChild1; 
        parent.updateWithBitmap(child, 3,3)
        #something like Assert.Equal(parent.getBitmap(), Properties.Resources.TestBitmapResult1); 



    }

Best Answer

Is it appropriate to store these bitmaps as resources

Well, whatever will work for you is appropriate, isn't it? Your unit tests should run quick, be functional, should be easy to maintain and to extend, and if they fail, they should make root cause analysis easier for you.

  • "quick" can be fulfilled by using very small bitmaps

  • "functional": to include significant cases, the bitmaps must be actually not too small. For your case, I guess bitmaps with a side length of minimum of 3 or 4 will probably make sense.

  • easy to maintain and to extend: well, as long as your tool set allows easy bitmap manipulation, storing of bitmaps in a resource and retrieving it again, that should not be much of an issue

  • "make root cause analysis easy": for this, you may have to make some provisions. If you just "check if the results of a bitmap comparison are equal", you might not easily see how your "testBitmap" looks like and why it does not match the expected results. But it should be pretty simple to add some code which in case the equality test fails, writes your test bitmap to disk so you can check it easily with an imager viewer (either just visually, or by creating something like a "difference image" between the expected bitmap and your test bitmap)

Of course, the alternative would be to create some per-pixel tests with hard-encoded colors and coordinates, but though that will surely work, IMHO that's not easier to maintain and can become very tedious.

Some related posts on other SE sites:

https://stackoverflow.com/questions/16217982/comparing-images-as-byte-arrays-for-unit-tests

https://sqa.stackexchange.com/questions/3136/how-to-test-the-graphical-contents-of-a-bitmap-or-a-pdf-file

Related Topic