Disable some SwiftLint rules for test target/unit tests files

9.7k Views Asked by At

I am looking to disable a couple of SwiftLint rules for the unit tests within my application.

For example I am wanting to disable the weak_delegate rule for my unit tests.

Having looked at the SwiftLint docs I think it may be possible by defining a custom weak_delegate rule and excluding the path to my unit tests.

https://github.com/realm/SwiftLint#defining-custom-rules

4

There are 4 best solutions below

1
On BEST ANSWER

You can disable them at a local level using:

//swiftlint:disable weak_delegate
let someDelete: someDelegate?
//swiftlint:enable weak_delegate

or at target level, by modifying your .swiftlint.yml file (hidden)

weak_delegate:
    excluded: ".*Test\\.swift" //regex path to your tests folder

or at project level, by modifying your .swiftlint.yml file (hidden)

disabled_rules:
 - weak_delegate
1
On

Best way to exclude some rules for test target is the nested configuration: you add a second .swiftlint.yml to root of your tests directory with rules to disable.

disabled_rules:
    - weak_delegate
    - cyclomatic_complexity
    - force_unwrapping
    - function_body_length
0
On

Add this to your .swiftlint.yml:

weak_delegate:
    excluded: ".*Test\\.swift" //regex path to your tests folder
0
On

Change the script in Build Phases to point only to the folder of your source code:

if which swiftlint > /dev/null; then
  swiftlint --path "$SRCROOT"/[name_of_source_folder]
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

Adding the flag --path "$SRCROOT"/path_to_folder will exclude the test targets.