Scoverage how to exclude files and packages?

10k Views Asked by At

I am trying to exclude packages and files from the scoverage report.

However it does not exclude the specified files and packaged.

I am doing this:

<excludedPackages>*api.test.*</excludedPackages> 
<excludedFiles>.*File.scala</excludedFiles>

Could someone suggest how it works for you ?

Cheers V.

3

There are 3 best solutions below

3
On

Similar to https://stackoverflow.com/a/44346781/2370679

Add the following to your build.sbt file:

To exclude packages (e.g. to exclude *api.test.* and *api.db* packages):

coverageExcludedPackages := "<empty>;*api.test.*;*api.db*"

You can also use the syntax to exclude files:

coverageExcludedPackages := "*File.scala"

For your specific example, you can try:

coverageExcludedPackages := "*File.scala;*api.test.*"

Reference: https://github.com/scoverage/sbt-scoverage#exclude-classes-and-packages

0
On

You seem to be doing this right, though I'm not familiar exactly with the Maven plugin which you seem to be using.

Thing is, when scoverage excludes files, it still compiles the classes in them, but it doesn't instrument them, resulting in a .class file identical to one which will be produced by compilation without the plugin. This might be confusing you to see .class files for classes originated in source files you explicitly excluded -- it sure did confuse me -- but this is how the plugin works.

Update:

The Scoverage scalac plugin will try to match file paths to the regex after removing .scala from them; i.e, omit the .scala from your regex as it will never match any file pattern.

Also note that the regex is matched against paths and not class names; i.e, use / instead of \.. Can't say if this works on Windows (if not then it's a bug with the scalac plugin).

0
On

In my build.sbt I was able to exclude classes using the following line:

coverageExcludedPackages := "<empty>;.*ExcludeThisClass;.*BuildInfo",

Note: The initial <empty>;was required for it to pickup on the first exclusion. Not sure why this is, but it matches the explanation by their readme: