altcover icon indicating copy to clipboard operation
altcover copied to clipboard

[FeatureRequest] Omitting some exception throwing branches

Open OnurGumus opened this issue 9 months ago • 2 comments

Is it possible to omit branches or functions where they throw some predefined exceptions as in NotSupportedException or NotImplementedException

I know I can get away with adding attributes and filter by it but that would pollute the code.

OnurGumus avatar Mar 14 '25 13:03 OnurGumus

In principle, pretty much anything is possible, dependent on how much decompilation is worth doing. I presume that you want to have behaviour like

if (condition)
{
 throw new ICannotDoThatException();
 // omit anything here from coverage (should give compiler warning anyway)
} // omit this closing bracket from coverage

Of course code like

var e = new ICannotDoThatException();
... // possibly many lines of code
if (condition)
{
 throw e;
 // omit anything here from coverage (should give compiler warning anyway)
} // omit this closing bracket from coverage

is not so simple to identify as such, even if it behaves similarly.

There's a related (and yet more expensive) feature request which is to note calls to methods flagged [DoesNotReturn] and behave similarly to one of the specified exceptions - expensive because every method called would have to be checked.


The approach we used while I was dealing with code coverage in production code while I was still wage-slaving was to do a pass with a static analysis rule, and and an method attribute of the form [UncoveredPoints(howMany, reasonWhy)] which is more flexible in that it's end-user tailored, and carries annotation suitable for code review purposes.

SteveGilham avatar Mar 14 '25 20:03 SteveGilham

I would settle to omit the method if the method just throws an exception directly with no branching in the simplest sense. That would be good.

OnurGumus avatar Mar 15 '25 02:03 OnurGumus