An Extension of Assert.AreEqual for images in MSTest


Unfortunately we dont have Assert. AreEqual for images, available in MStest (correct me if I'm wrong).

While performing automation using C#, we have to compare two images downloaded from target website and images stored at some shared location. To verify this we don't have standard assert provided by MStest.

Then we thought to write our own assert class for images. Which will compare the images and based on condition it will assert. Now it's time to share with all automation community.

So here is the piece of code to perform this.

First of all I will write Test Method which will compare two images with assert.

using System;

using System.Drawing;

using System.Text;

using System.Collections.Generic;

using System.Linq;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using AssertImage;

 

namespace TestAssertImage

{

    [TestClass]

    public class UnitTest1

    {

        [TestMethod]

        public void TestImagesEqual()

        {

            const string imageLocation1 = @"C:\Users\xmdj\Desktop\Images Of Talk2DB\Connect.PNG";

            const string imageLocation2 = @"C:\Users\xmdj\Desktop\Images Of Talk2DB\Connect1.PNG";

            var bitmapExpected = new Bitmap(imageLocation1, true);

            var bitmapActual = new Bitmap(imageLocation2, true);

            Assert.IsNotNull(bitmapExpected, "Not Null");

            Assert.IsNotNull(bitmapActual, "Not Null");

            AssertImage.AssertImage.AreEqual(bitmapExpected, bitmapActual, "Not Same!!");

 

        }

 

    }

}

The highlighted part in above code is our extension to assert for images.

And below is our extension class for AssertImage.AreEqual.

using System.Drawing;

using System.Security.Cryptography;

using Microsoft.VisualStudio.TestTools.UnitTesting;

 

namespace AssertImage

{

    public class AssertImage

    {

        public static void AreEqual(Bitmap expected, Bitmap actual, string message)

        {

            string asserName = "AssertImage.AreEqual";

            //Test to see if we have the same size of image

            if (expected.Size != actual.Size)

            {

                throw new AssertFailedException(

                   asserName + " failed. Expected:<Height " + expected.Size.Height + ", Width" + expected.Size.Width +

                                 ">. Actual:<Height " + actual.Size.Height + ",Width " + actual.Size.Width + ">. " +

                                  message);

            }

            //Convert each image to a byte array

            ImageConverter ic = new ImageConverter();

            byte[] btImageExpected = new byte[1];

            btImageExpected = (byte[])ic.ConvertTo(expected, btImageExpected.GetType());

            byte[] btImageActual = new byte[1];

            btImageActual = (byte[])ic.ConvertTo(actual, btImageActual.GetType());

 

            //Compute a hash for each image

            var shaM = new SHA256Managed();

            byte[] hash1 = shaM.ComputeHash(btImageExpected);

            byte[] hash2 = shaM.ComputeHash(btImageActual);

 

            //Compare the hash values

            for (int i = 0; i < hash1.Length && i < hash2.Length; i++)

            {

                if (hash1[i] != hash2[i])

                    throw new AssertFailedException(

                     string.Format(asserName + " failed. Expected:<hash value " + hash1[i] + ">. Actual:<hash value " + hash2[i] + ">. " +

                                   message));

            }

        }

 

    }

}

Feel free to provide your comments and feedback.


Similar Articles