locked
Assert | Code Coverage RRS feed

  • Question

  • Hello Team , 

    Have written a unit test method using the fakes to remove the external dependency and it is working fine but what is worrying me is the code coverage results is that it is showing the  "Assert,.IsEqual" method in the unit test as not covered ?

    IS it expected behavior from the code coverage ? If yes, then how to avoid this as i need Assert to test the results ? 

    Any pointers are highly appreciated 

    Regards,

    Rajesh

    Monday, July 22, 2013 4:17 AM

Answers

  • Hi RajeshSql,

    Thank you for your post!

    I tried a sample as follows:

    namespace FackLib
    {
        public interface IDataAccess
        {
            int Read();
        }
    }


    namespace FackLib
    {
        public class MyDataAccess:IDataAccess
        {
            public int Read()
            {
               return Tools.Num();
            }
        }
        public class Tools
        {
            public static int Num()
            {
                return 1;
            }
        }
    
        public class MyClass
        {
            public static int GetMyData(IDataAccess obj)
            {
                return obj.Read();
            }
            public void Data()
            {
    
            }
        }
    }


    namespace UnitTestProject1
    {
        [TestClass]
        public class UnitTest2
        {
            [TestMethod]
            public void StubTest()
            {
                IDataAccess stockFeed = new FackLib.Fakes.StubIDataAccess() { Read = () => { return 2; } };
                Assert.AreEqual(2, MyClass.GetMyData(stockFeed));
            }
            [TestMethod]
            public void ShimTest()
            {
                using (ShimsContext.Create())
                {
                    FackLib.Fakes.ShimMyClass.GetMyDataIDataAccess = (inc) => { return 2; };
                    Assert.AreEqual(2, MyClass.GetMyData(null));
                }
            }
    
        }
    }

    The result is:

    It indicates that Assert statement can be code coveraged.

    Code coverage is used to measure how much code is tested. If one line of code is not covered, I think that maybe it is not executed. Please check if the Assert.IsEqual is executed when you run test. In addition, as my experience, when there are mutiple Assert statements in the test method, if one Assert failed, test will stop and the rest of Assert statements will not be executed.

    Thanks

    Best Regards




    Tuesday, July 23, 2013 10:03 AM
  • Hello,

    I am marking useful reply as answer. If the suggestion can’t help you, you can come back and unmark it as answer. If you have anything unclear, please feel free to come back and ask me.

    Best regards,


    Amanda Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by RajeshSql Wednesday, July 31, 2013 7:15 AM
    Wednesday, July 31, 2013 5:37 AM

All replies

  • Can some one share the info please
    Monday, July 22, 2013 9:14 PM
  • Hi RajeshSql,

    Thank you for your post!

    I tried a sample as follows:

    namespace FackLib
    {
        public interface IDataAccess
        {
            int Read();
        }
    }


    namespace FackLib
    {
        public class MyDataAccess:IDataAccess
        {
            public int Read()
            {
               return Tools.Num();
            }
        }
        public class Tools
        {
            public static int Num()
            {
                return 1;
            }
        }
    
        public class MyClass
        {
            public static int GetMyData(IDataAccess obj)
            {
                return obj.Read();
            }
            public void Data()
            {
    
            }
        }
    }


    namespace UnitTestProject1
    {
        [TestClass]
        public class UnitTest2
        {
            [TestMethod]
            public void StubTest()
            {
                IDataAccess stockFeed = new FackLib.Fakes.StubIDataAccess() { Read = () => { return 2; } };
                Assert.AreEqual(2, MyClass.GetMyData(stockFeed));
            }
            [TestMethod]
            public void ShimTest()
            {
                using (ShimsContext.Create())
                {
                    FackLib.Fakes.ShimMyClass.GetMyDataIDataAccess = (inc) => { return 2; };
                    Assert.AreEqual(2, MyClass.GetMyData(null));
                }
            }
    
        }
    }

    The result is:

    It indicates that Assert statement can be code coveraged.

    Code coverage is used to measure how much code is tested. If one line of code is not covered, I think that maybe it is not executed. Please check if the Assert.IsEqual is executed when you run test. In addition, as my experience, when there are mutiple Assert statements in the test method, if one Assert failed, test will stop and the rest of Assert statements will not be executed.

    Thanks

    Best Regards




    Tuesday, July 23, 2013 10:03 AM
  • Hello,

    I am marking useful reply as answer. If the suggestion can’t help you, you can come back and unmark it as answer. If you have anything unclear, please feel free to come back and ask me.

    Best regards,


    Amanda Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by RajeshSql Wednesday, July 31, 2013 7:15 AM
    Wednesday, July 31, 2013 5:37 AM