Checkstyle different rules for different files
I have one file that contains the rules for a project. I want my unit test methods to be able to have underscores in their names. How myMethod_should_call_someClass_someMehod
. I currently have one configuration that applies to all project files.
My question is whether it is possible to somehow customize the checkstyle, so, for example, I specify specific rules for all files ending in *Test.java
.
Currently the only solution I have found is to provide SuppressionFilter
and exclude all files ending in *Test.java
. But is there a way to provide another module MethodNameCheck
with a different format for the test files?
source to share
You have to define MethodName twice , with one instance testing the normal methods and the other testing the test methods. Notice the property id
we'll be using to restrict checks to their respective domains:
<module name="MethodName">
<property name="id" value="MethodNameRegular"/>
<property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
</module>
<module name="MethodName">
<property name="id" value="MethodNameTest"/>
<property name="format" value="^[a-z][a-zA-Z0-9_]*$"/>
</module>
Then regular checking should be suppressed for test methods and vice versa. This only works if you have criteria to differentiate between the two types of classes. I am using the Maven layout convention which puts regular classes under src/main
and tests the classes in src/test
. Here is the suppression filter file:
<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress files="[\\/]src[\\/]test[\\/].*" id="MethodNameRegular" />
<suppress files="[\\/]src[\\/]main[\\/].*" id="MethodNameTest" />
</suppressions>
source to share