Using .runsettings to exclude assemblies from code coverage

The issue is the period. For some reason the RegEx is choking on that. You can get around it by escaping the period as follows:

<ModulePaths>
    <Include>
        <ModulePath>.*MyCompany\.Namespace\.Project\.dll$</ModulePath>
    </Include>
    <Exclude>
        <ModulePath>.*ThirdParty\.Namespace\.Project\.dll$</ModulePath>
    </Exclude>
</ModulePaths>

Also, the empty includes are valid and imply that all Modules are included. The comments of the Microsoft provided sample state that:

<!--
About include/exclude lists:
Empty "Include" clauses imply all; empty "Exclude" clauses imply none.
Each element in the list is a regular expression (ECMAScript syntax).
See http://msdn.microsoft.com/library/2k3te2cs.aspx.
An item must first match at least one entry in the include list to be included.
Included items must then not match any entries in the exclude list to remain included.
-->

As I couldn't find this answer anywhere else, and this just took me a while to figure out, ModulePath is the full path, and you may be matching your pattern somewhere else in the path.

For example, if you have a project Foo and a project Foo.Tests, and they are built to their own directories, you will end up with Foo.Tests\bin\Release\Foo.dll and Foo.Tests\bin\Release\Foo.Tests.dll. This is the dll that the test assembly will reference, so this is the path that is used. Foo\bin\Release\Foo.dll is not directly referenced by the test assembly.

If you try to exclude .*tests.* it will match both paths and produce no coverage.

To only exclude assemblies with "test" in their file name, ignoring their path, I used

<Exclude>
  <ModulePath>.*\\[^\\]*test[^\\]*\.dll</ModulePath>
</Exclude>

On a related note, I ran into this post because I was bit thinking clearly about the regular expression nature of the include and exclude paths. For my WPF application, I wanted to exclude coverage analysis on Caliburn.Micro. So I had written

<ModulePath>Caliburn.Micro.dll</ModulePath>

Clearly, the period is messing me up. This question does not suffer from that problem, but I bet I’m not the only one to overlook this simple fact. For any other readers, please also take note that * is not a wildcard – it is the regular expression “any number of” operator. You do not want *.Caliburn, but rather .*Caliburn Thus this simple statement solved my problem:

<ModulePath>.*Caliburn.*</ModulePath>

Because it is looking for a path, not just a module name, you need the .* in front of the module to ignore it – that is, you want to ignore it at any given file path.