Translate

Thursday 11 October 2012

A simple coverage example - NCover 4 - Agile series I

The failing test, TestMultiply, decreases the coverage % plus the two methods, Subtract and Divide, do not have unit tests, which also decreases the coverage. 


    [TestFixture]
    class TestCoverage_Class2
    {
        Coverage_Class1 initCoverage = new Coverage_Class1(55, 12);
        [Test]
        public void TestAdd()
        {
            Assert.AreEqual(67,initCoverage.Add());
        }
        [Test]
        public void TestMultiply()
        {
            Assert.AreEqual(60, initCoverage.Multiply());
        }
    }

// Domain class being tested

public class Coverage_Class1
    {
        int firstValue, secondValue;
        public Coverage_Class1(int a, int b)
        {
            firstValue = a;
            secondValue = b;
        }
        public int Add()
        {
            return firstValue + secondValue;
        }
        public int Multiply()
        {
            return firstValue * secondValue;
        }
        public int Subtract()
        {
            return firstValue - secondValue;
        }
        public int Divide()
        {
            return firstValue / secondValue;
        }
    }

Write the two tests for the additional two methods, Divide and Subtract, with one of them a failing test and the coverage looks like this:



Make them all pass and voila, 100 % coverage  !




Work around with the Threshhold values and you will increase your own coverage capabilities ! :) Happy Coverage! :)


No comments: