100% code coverage or is it really

Hi wrote a little application that I wanted demonstrate code coverage features of MS TEST.

public interface IDataAccess
{
List<string> GetUsers(bool includeInactive);
}
public class BusinessLogic
{
private IDataAccess m_DataAccess;
public BusinessLogic(IDataAccess da)
{
m_DataAccess = da;
}
public List<string> ListUsers(bool includeInactive)
{
return m_DataAccess.GetUsers(includeInactive);
}
}
[TestMethod()]
public void TestListUsers()
{
MockRepository mockery = new MockRepository();
IDataAccess mockDataAccess = mockery.CreateMock<IDataAccess>();
BusinessLogic bl = new BusinessLogic(mockDataAccess);
bool includeInactive = true;
List<string> expectedList = new List<string>();
List<string> returnedList;
expectedList.Add("1");
using (mockery.Record())
{
Expect.Call(mockDataAccess.GetUsers(includeInactive)).Return(expectedList);
}
using (mockery.Playback())
{
returnedList = bl.ListUsers(includeInactive);
}
Assert.Equals(returnedList, expectedList);
}
}



code coverage results are (100%):



Mezer_02-14_01-14-45



Take a minute and look again at the above code ( review it :) ).



The big issue in the above test is that we didn't take in mind the input parameter to the BL that also HAVE to be passed to the DAL.



So actually we forgot one important test - Testing that when we pass includeInactive = False to the BL it will also be passed to the DAL, as a result a user might change the behavior of the BL to always pass True and we would never find it in the test.



In a nut shell 100% code coverage using MSTest  (also other programs) means nothing!  if the input parameters are not being tested.



Another important issue to remember is that if you method doesn't throw an exception (swallow it) than you must add a test that this method will never throw exception - that will be a proactive test so that other developer wont decide to throw and exception in a running code (and actually changing the interface) without knowing the outcome)

100% code coverage or is it really 100% code coverage or is it really Reviewed by Ran Davidovitz on 3:25 PM Rating: 5

No comments:

Powered by Blogger.