NCover Documentation

Please visit Pre-Coverage Filters for updated information.

NCover v3.0 (Complete) supports 4 categories of pre-coverage includes and excludes. * Include/Exclude Assemblies (.exe and .dll files) * Include/Exclude Source Files * Include/Exclude Types (primarily classes, but also namespaces, structs, and enums) * Exclude Methods

For ease of use, all regular expressions are applied without regard to case (i.e. 'A' matches both 'a' and 'A'). NCover uses Perl Compatible Regular Expressions (more information, including syntax can be found here).

What is an exclusion rule?

An exclusion rule is a regular expression that NCover tries to match against every element in the same category as the rule. For example, NCover tries to match an assembly exclusion rule against the name of every assembly that your program uses; NCover tries to match a type exclusion rule against the fully-qualified name of every type - class, namespace, enum, struct - that your program uses. If the rule matches the name of the assembly or the type NCover excludes the item from coverage and does not gather coverage data on the assembly or the type.

What is an inclusion rule?

An inclusion rule is the opposite of an exclusion rule. If the name of an assembly, type, source file or method matches the inclusion rule, then NCover includes the item in coverage. If the name does not match, then NCover excludes the item from coverage and does not gather coverage data on it. By default, NCover gathers coverage on everything.

In what order are pre-coverage rules applied?

Inclusion rules are always applied before exclusion rules on a per category basis. For example, a type inclusion rule would be applied before a type exclusion rule, but not before an assembly exclusion rule. Applying inclusion rules before exclusion rules lets you succintly control which elements are covered and which are not. You could have an assembly inclusion rule that includes all of the assemblies in your project (so 3rd-party dlls aren't covered) and then add an assembly exclusion rule to exclude the unit test assemblies in your project.

In addition to applying inclusion rules before exclusion rules, NCover also applies rules to code elements in the order that it receives them from the CLR. The .NET CLR first gives NCover the assemblies that a program/executable uses, then the types, then the methods, then the source files for each method. As NCover receives each code element from the CLR, NCover figures out if it should gather coverage data on the element or not. If NCover decides that it shouldn't gather coverage data on the element, then NCover doesn't gather coverage data on its children either. That is, if an assembly is excluded, then none of the types or methods or source files in the assembly will be covered, even if one of them is matched by an inclusion rule. The reason none of the assembly's children will be included is because NCover greedily applies inclusion and exclusion rules -- so once a class is excluded, its methods and inner types are never even considered for inclusion, but excluded without even consulting the inclusion and exclusion rules.

What is the exact logic that NCover uses to exclude something from coverage?

Since the logic NCover uses to figure out if a code element should be included or excluded is difficult to clearly express in English, let's look at the logic in pseudocode.

for each assembly in the covered program
  is the assembly "mscorlib.dll"? exclude the assembly
  is the assembly auto-generated (e.g. xml serialization assemblies)? exclude the assembly

  does one of the default exclusions in NCover.Console.exe.config match the name of the assembly? exclude the assembly
  do none of the assembly inclusion rules match the name of the assembly? exclude the assembly
  does the assembly name match one of the assembly exclusion rules? exclude the assembly

  for each class in the assembly // the CLI doesn't know anything about namespaces
    is the class's assembly excluded? then exclude the class


    not do any of the attributes on the class match at least one of the attribute inclusion rules? then exclude the class
    do any of the attributes on the class match at least one of the attribute exclusion rules? then exclude the class

    // visit /knowledge-base/what-is-the-fully-qualified-name-of-a-class/ for an explaination of what a 
    // class's "fully qualified" name is.
    do none of the type inclusion rules match the fully qualified name of the class? exclude the class
    do any of the type exclusion rules match the fully qualified name of the class? exclude the class

    do none of the attribute exclusion rules match the fully qualified name of the class> exclude the class
    do any of the attribute exclusion rules match the fully qualified name of the class? exclude the class

    if the class is an inner class, are any of its parents excluded? exclude the class

    for each method in the class
      is the method's class excluded? exclude the method

      (not (do any of the attributes on the method match at least one of the attribute inclusion rules))? then exclude the method
      do any of the attributes on the method match at least one of the attribute exclusion rules? then exclude the method

      do none of the method inclusion rules match the fully qualified name of the method? exclude the method
      do any of the method exclusion rules match the fully qualified name of the method? exclude the method

      for each source file in the method
        is the source file already excluded? then exclude it
  
        do none of the source file inclusions match the fully qualified path of the source file? exclude the source file
        do any of the source file exclusions match the fully qualified path of the source file? exclude the source file

      end for each
    end for each

  end for each
end for each

If you still need technical assistance, we can help:

Submit A Support Ticket