I've recently become very interested in improving my project's test coverage. At this point, I have a submodule pretty thoroughly tested, but it's coverage is still pretty low because the tool is picking up all of my views as entirely uncovered. (I'm using MVP, so these views are really "dumb", they don't need to be tested in my opinion.) Before I go slapping an [ExcludeCodeCoverage] attributes on my views, I'd like to know if there are any downsides to this.

To be clear about things, I'm only looking to do this at the class level. For example, I have a method that hits the file system. Obviously, I can't test it (easily), but I consider the fact that I can't test it a smell. Since it's a smell, the reduced coverage on this class is an alert to me that there's something in need of refactoring. I will not be excluding anything "just to get the numbers up". I'm just interested potential pitfalls of excluding the UI layer and simple class factories like this.

public class SourceControlProviderFactory : ISourceControlProviderFactory
    public ISourceControlProvider CreateProvider(VBProject project)
        return new GitProvider(project);

    public ISourceControlProvider CreateProvider(VBProject project, IRepository repository)
        return new GitProvider(project, repository);

This question is closely related, but I've already targeted the code I'd like to exclude. I'm more interested in "Are there good reasons not to do this?" as opposed to "What should I exclude?".

  • There aren't any hidden monsters, if that's what you're asking. Excluding code simply means that such code does not participate in the coverage analysis, as you may have already surmised. You probably ought to have some tangible policy for excluding code; i.e. it shouldn't be an arbitrary decision, but should be based on specific rules. Commented Jun 13, 2015 at 15:26
  • Yeah. That's what I'm wondering @RobertHarvey. I want to make sure there aren't any "hidden monsters" waiting for me if I do this.
    – RubberDuck
    Commented Jun 13, 2015 at 15:27

1 Answer 1


What are the cons to excluding some code from coverage analysis?

  1. You don't get to see that code in your code coverage report. So if the coverage changes, you won't know about it (from this tool), so you won't fix it.

  2. Some of your... less disciplined programmers (which may be you in the future) will be more inclined to exclude code if they see other places which are excluded.

But the attribute is there for a reason. Some code, you just don't care if it has test coverage because it's obviously correct, or because it clutters up your report, making it harder to see things that actually are missing coverage. So use it judiciously, and make sure that others do as well.

  • You've given me a good idea. I can create a Diagnostic that looks for classes with the attribute in places it may not belong. I think you hit the nail on the head. It's that discipline I think I was worried about. Thank you.
    – RubberDuck
    Commented Jun 13, 2015 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.