I’m using xUnit to test my C# code and I’m using Visual Studio Premium 2012.
In my solution I have my main project that I’m testing and a 2nd project that contains all of my tests. I’m supposesd to be at 100% code coverage, but there are some functions in my Test project that I cannot get to 100%. Can I just exclude that project from appearing in Code Coverage results?
Or… does anyone now how to get a test function to 100% when you have a test where you are expecting an exception to be thrown? Here are some of the ways I’ve tried to write a test for a method that should throw an exception and what isn’t being covered. MyBusinessLogic has a function named GenerateNameLine that accepts an object of type MyViewModel. if the Name property of MyViewModel is an empty string, it should throw an exception of type RequiredInformationMissingException.
[Fact]
public void TestMethod1()
{
var vm = new MyViewModel();
vm.Name = string.Empty;
Assert.Throws<RequiredInformationMissingException>(delegate { MyBusinessLogic.GenerateNameLine(vm); });
}
This test passes, but code coverage with color highlighting it showing me that MyBusinessLogic.GenerateNameLine(vm); is not getting hit.
I’ve also tried:
[Fact]
public void TestMethod1
{
bool fRequiredInfoExceptionThrown = false;
var vm = new MyViewModel();
vm.Name = string.Empty;
try
{
MyBusinessLogic.GenerateNameLine(vm);
}
catch (Exception ex)
{
if (ex.GetType() == typeof(RequiredInformationMissingException))
fRequiredInfoExceptionThrown = true;
}
Assert.True(fRequiredInfoExceptionThrown, "RequiredInformationMissingException was not thrown.");
}
This test also passes. But code coverage says the } right before my catch is never hit.
I don’t know how to write a test for an exception that gets 100%. I know it doesn’t even really matter, but at work 100% code coverage is part of our definition of done, so I don’t know what to do here.
The answer is Yes
We provide filters to customize what you want to include/exclude via the .runsettings file. You can filter out pretty much anything that you do not find useful.
The [ExcludeFromCodeCoverage] attribute can also be used in code.
See: http://blogs.msdn.com/b/sudhakan/archive/2012/05/11/customizing-code-coverage-in-visual-studio-11.aspx
Are you seeing the second issue in VS2012RTM+Update1 as well?