NCover code coverage result for deliberately failing tests
I have a couple of unit test helper extension methods, such as, IsNotEmpty(this string str)
for which I have also written unit tests. The basic structure o开发者_JS百科f these tests is that I have one test that verifies that the extension method passes and returns the expected result on a "good" input, and one that verifies that a fail exception is thrown on "bad" input. For example, the fail tests for the above method might look like this:
[TestMethod, Fails]
public void IsNotEmpty_Fails_On_Empty_String()
{
"".IsNotEmpty();
}
where Fails
is an attribute I wrote myself - basically a proxy for [ExpectedException(typeof(AssertFailedException))]
. So I'm explicitly expecting an exception to be thrown on the only line in the above test, and thus, I'm not expecting the method to run all the way.
But when I right-click my solution, select "Test with->NCover", I get less than 100% code coverage* on all methods like the above one, with the ending brace highlighted red.
Why does NCover complain about my test method not finishing, when I've said that it won't?
**) For all those of you who will reply that 100% code coverage is not necessary: No, I'm not particularily worried about maintaining a 100% code coverage at all costs. However, in these cases methods that* do matter show up as not covered in the list, and I have to go all the way into the tree to see that it's just them. That's the issue I'm trying to get at here - not 100% code coverage in general.
Are you running a debug build? That's probably the issue. NCover works off of the debugging breakpoints, and Microsoft adds one to the closing bracket of each method so it can step there before leaving the method. Running in release mode should take care of those closing brackets for you.
Don't worry about 100% code coverage. Just get as close as you reasonably can.
Attaining 100% code coverage is a waste of time.
All that matters is that you are confident in your tests covering the code that needs to be covered.
You can have a solid set of tests that only cover 70% of your code.
Your best tests likely only cover a few lines, but they are the ones that will prevent most of the bugs. They are the 'edge case tests'. The ones that without them, the other tests are useless. Your entire test suite is useless, as they are the cases that test the mistakes that would typically keep getting reintroduced into code base if they didn't exist.
精彩评论