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)
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>();
returnedList = bl.ListUsers(includeInactive);
code coverage results are (100%):
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)