KoffeeKoder


  • Hacking Code Coverage
    published on 7/14/2008 3:16:01 PM
  • Code Coverage represents how much of the code is covered under unit tests. The more code coverage the more the developer will have confidence in his/her code. Unfortunately, code coverage cannot be taken as the only metric when calculating the design and architecture of an application. Code coverage should follow with code reviews, pair programming and other agile mythologies.
    I also think that the code coverage shows the honesty in the developer. This is because you can easily HACK it and get 100% coverage. Here is a little example of achieving 100% code coverage by cheating.
    We have a simple Customer class:

    public class Customer
        {
            private int _id;
            private string _firstName;
            private string _lastName;
            private DateTime _dateCreated;

            public int Id
            {
                get { return _id; }
                set { _id = value; }
            }

            public string FirstName
            {
                get { return _firstName; }
                set { _firstName = value; }
            }

            public string LastName
            {
                get { return _lastName; }
                set { _lastName = value; }
            }

            public DateTime DateCreated
            {
                get { return _dateCreated; }
                set { _dateCreated = value; }
            }
        }
    }

    We need to write unit tests so that we have 100% coverage of the Customer class which resides in the Demo.Domain assembly. Here is the unit test which will achieve the maximum coverage.

    [Test]
            public void will_fake_out_100_percent_test_coverage()
            {
                Assembly assembly = Assembly.Load(_assemblyName);

                Type[] types = assembly.GetTypes();

                foreach (Type type in types)
                {
                    PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Static
                | BindingFlags.Instance);

                    foreach (PropertyInfo property in properties)
                    {
                        object o = Activator.CreateInstance(type);
                        property.GetValue(o, null);
                        property.SetValue(o, null, null);                    
                    }

                    // do the same thing for the methods!

                }

            }

    In the code above I am performing the get/set operations on the Customer class properties. This will result in 100% code coverage. But will this coverage be any useful? I don’t think so, since I am simply hacking my way to maximize the code coverage.  The unit test does not serve any good purpose.
    There are couple of ways to stop fake code coverage which includes code reviews, pair programming and random raid by a senior developer but at the end it all depends on the developer’s honesty.  I think if we use code coverage with the intention of writing good code then it is a very good metric and can help to create better applications.