FindBugs filter file for ignoring JUnit tests
Asked Answered
P

2

18

I need to set up a filter file for my findbugs ant script that scans only the src/* files and not the test/* files.

What is the syntax for checking all classes while ignoring any filename or package name with 'test' in the name?

Photogravure answered 16/4, 2009 at 14:56 Comment(0)
E
25

FindBugs is actually scanning the compiled class files, not the sourcePath. If you are compiling your src/* and test/* files to the different directories, you could just use the nested <class...> element.

<findbugs home="${findbugs.dir}" output="xml:withMessages" 
    outputFile="${findbugs.report.xml}" jvmargs="-Xmx256M" 
    effort="max" projectName="${ant.project.name}" 
    auxClasspathRef="findbugs.classpath" 
    sourcePath="${src.dir}">
  <class location="${src.classes.dir}"/>
</findbugs> 

That won't work if src/* and test/* are both compiled to a single directory. In that case, use a filter file and exclude the packages or class names that correspond to tests.

<findbugs home="${findbugs.dir}" output="xml:withMessages" 
    outputFile="${findbugs.report.xml}" jvmargs="-Xmx256M" 
    effort="max" projectName="${ant.project.name}" 
    auxClasspathRef="findbugs.classpath" 
    sourcePath="${src.dir}"
    excludefilter="exclude.xml">
  <class location="${classes.dir}"/>
</findbugs> 

where exclude.xml looks like:

<FindBugsFilter>
  <Match>
    <Class name="~.*Test$"/>
  </Match>
  <Match>
    <Package name="~test\..*"/>
  </Match>
</FindBugsFilter>
Ethnomusicology answered 16/4, 2009 at 16:0 Comment(1)
good explanation... apparently I need to start drinking coffee or something. Deleted my answer, which well correct is misleading.Piano
N
-4

By the way, it is a good idea to cover unit tests with FindBugs as well. There is no reason for using lower quality standards towards tests. Bugs in test are just that, bugs.

Sure, if you run FindBugs first time, there might be many bug reports, but the bug count will come down overtime if you pay any attention to them.

Nieman answered 12/6, 2010 at 3:29 Comment(3)
The problem with this is that you test the error cases in the unit test (such as passing null as a parameter that is marked @Nonnull)Fullfledged
Why would you need to test that null case when you have your findbugs set up?Carvalho
@konstantin.zaikin because you want to know what might happen at runtime too, not just what can be deduced at compile timeBarcot

© 2022 - 2024 — McMap. All rights reserved.