What is the recommended way to exclude auto-generated code from NCrunch code-coverage?
At the moment I've got a number of classes that are generated using T4 Templates. The partial classes are currently preceded with the following standard Microsoft comments:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
At the moment, there is no feature that will let you handle this directly on a macro-level. You're welcome to request one on uservoice if you like.
There are some other options below that might get you close to what you are after:
- You can turn off the code coverage for an entire project/assembly by turning off the 'Instrument output assembly' project-level configuration setting
- It's possible to turn off code coverage using comments in the code. If its possible to change the auto generation template this could work well - http://www.ncrunch.net/documentation/concepts_code-coverage-suppression.
- If the generated code is all under the same namespace, you can exclude it from metrics aggregation in the Metrics Window so that it won't be applied to your metrics totals. I suggest this as I assume the reason you want to exclude this code is to remove its impact on your metrics.
I am generating the code myself so I think adding comments to the code will be the easiest solution. As I am currently just on an evaluation licence ATM I was trying to keep the code as NCrunch independent as possible, but this should be fine.
we are using a similar approach (generating classes via T4 templates).
To exclude them from code coverage (not only NCrunch but also dotCover, MSTest, NCover, etc.), we add/generate the [ExcludeFromCodeCoverage] attribute to the generated classes.