29

Is there a way to exclude Pods from Code Coverage?
I would like to see Code Coverage only for the code I've written.

Not that it should matter, but I'm using Xcode 8.

1

8 Answers 8

36

These steps will help:

1. add these lines to Podfile

# Disable Code Coverage for Pods projects
post_install do |installer_representation|
    installer_representation.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
        end
    end
end

2. run pod install

Now you won't see pods in test coverage.

Note: It only excludes Objective-c pods but not Swift

2
  • Fab solution thanks! how can you exclude 'C' files if they exist in the pods as well ?
    – DrPatience
    Commented Jul 20, 2017 at 10:59
  • How do you exclude swift files also? Anyone?
    – l.vasilev
    Commented Mar 19, 2018 at 15:25
23

XCode 10 Update

In Xcode 10 you can set which targets you want to enable code coverage for in

Edit Schemes > Test > Options

Just select 'Gather coverage for some targets' and add your main project.

2
  • 1
    Man U are Legend 10+
    – Jack
    Commented Sep 23, 2020 at 6:43
  • on Xcode 15.4 this option disappear, anyone know how can I set it?
    – sidneivl
    Commented Jun 1 at 13:33
10

To disable coverage for swift code you can use a wrapper for SWIFT_EXEC (I verified this so far with Xcode 9.3). Hence the complete solution (incl. Swift) would be the following:

Append to your Podfile (and invoke pod install after that):

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |configuration|
      configuration.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
      configuration.build_settings['SWIFT_EXEC'] = '$(SRCROOT)/SWIFT_EXEC-no-coverage'
    end
  end
end

Place the following script (name it SWIFT_EXEC-no-coverage) at the root of your source tree (chmod +x as necessary):

#! /usr/bin/perl -w

use strict;
use Getopt::Long qw(:config pass_through);

my $profile_coverage_mapping;
GetOptions("profile-coverage-mapping" => \$profile_coverage_mapping);

exec(
    "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc",
    @ARGV);

Here's a link to the corresponding gist: https://gist.github.com/grigorye/f6dfaa9f7bd9dbb192fe25a6cdb419d4

1
  • 2
    Worked for me. Once small note, the file needs to be placed in the Pods directory for this to work. For project root, change to '$(SRCROOT)/../SWIFT_EXEC-no-coverage' Commented Jun 18, 2018 at 20:09
5
  1. Click on your Pods project in the Project Navigator on the left
  2. On the right hand side, open project and target list if it's not already open; then click on the Pods project name (NOT the targets).
  3. Click Build Settings.
  4. In the search bar, search for "CLANG_ENABLE_CODE_COVERAGE".
  5. Change "Enable Code Coverage Support" to NO.
  6. Re-run test.
1
  • 7
    You shouldn't ever be changing the Pods project as these settings will get lost on next pod install/update. The solution @tung-fam is the correct way of doing it.
    – Camsoft
    Commented Feb 13, 2017 at 9:37
3

If you are developing a pod and want to have code coverage just for yours:

    # Disable Code Coverage for Pods projects except MyPod
    post_install do |installer_representation|
      installer_representation.pods_project.targets.each do |target|
        if target.name == 'MyPod'
          target.build_configurations.each do |config|
            config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'YES'
          end
        else 
          target.build_configurations.each do |config|
            config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
          end
        end
      end
    end
1
  • 1
    You could also just add next if target.name == 'MyPod' as the 3rd line of the answer @tung-fam gave.
    – ChrisH
    Commented May 25, 2018 at 16:36
0

Based on answer of @Tung Fam adding exclusion list for some pods

# Disable Code Coverage for projects listed in excluded_pods

excluded_pods = ['Pod1', 'Pod2', 'Pod3']

post_install do |installer_representation|
    installer_representation.pods_project.targets.each do |target|
        next if !excluded_pods.include?(target.name)

        target.build_configurations.each do |config|
            config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
        end
    end
end
0

Use this code to remove it works for both Swift and objc Pods

 post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
          
          # remove code coverage
          config.build_settings['CLANG_ENABLE_CODE_COVERAGE'] = 'NO'
          config.build_settings['ENABLE_CODE_COVERAGE'] = 'NO'
        end
    end
  end
end
0

If you are facing issues while trying to configure code coverage in Xcode 15, follow the steps below to resolve the issue:

  • Due to reputation limitations, I am unable to upload images directly to this post. Please click on the following link to view the images
  1. Edit Schemes. With the Test Scheme selected, go to the Test Plan section and click on the arrow:

  2. Go to the Configurations tab, find the Code Coverage section. In the Code Coverage section, change the option to some target. And add the targets for which you want to obtain code coverage:

2
  • 1
    This seems to present exactly the same solution as this 5 year old answer with minor UI changes.
    – miken32
    Commented Jun 5 at 15:22
  • @miken32, you are correct that the underlying solution is the same. However, I posted this to demonstrate how the implementation looks with the UI changes introduced in Xcode 15. Commented Jun 6 at 13:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.