Code Coverage Suppression on Next Line Only
On unit tests where you check for expected exceptions the end of the method can't be reached, so the line with the closing curly bracket keeps uncovered.
Placing a no coverage comment directly after the closing curly bracket works.
If you reorganize your code by using automated tools such like CodeMaid or ReSharper, the no coverage comment does not stay after the designated line and you have to adjust that by hand again (and again...).
Introducing a suppression comment like
"// ncrunch: no coverage next"
which can be placed directly over the closing curly brackets would be moved as expected, because it is inside the code block (the method) which is moving.
-
Oliver Abt commented
This is about unit tests in the form:
[TestMethod, ExpectedException(typeof(InvalidOperationException)]
public void InvalidOperaionTest()
{
MethodUnderTest(); // should throw the exception in the current state// lines after that point can not be reached on a successfull test
// with the current behaviour of NCrunch this line would be marked as uncovered
// something like the following should suppress this:// ncrunch: no coverage next
}It looks like this is negligible because per such a test there is only one line of uncovered code, but source files wich are not at 100% coverage make me recheck them ever and ever again.
I'm aware that I could write my test like that:
[TestMethod]
public void InvalidOperaionTest()
{
InvalidOperationException expectedException = null;
try
{
MethodUnderTest(); // should throw the exception in the current state} // ncrunch: no coverage
catch(InvalidOperationException ex)
{
expectedException = ex;
}Assert.IsNotNull(expectedException);
}but this looks very bloated to me.
-
Kieren Johnstone commented
Do you mean coverage in the test itself, or the unit under test?
If the test itself, I can't imagine any statements after the line expected to throw and exception (and you're in control of this). If in the unit under test, shouldn't a different test check the non-exception code path?