How to exclude Pods from Code Coverage in Xcode
Asked Answered
A

8

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.

Alienism answered 24/9, 2016 at 8:7 Comment(1)
Similar question here: #40102512Waterresistant
E
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

Everick answered 12/1, 2017 at 18:44 Comment(2)
Fab solution thanks! how can you exclude 'C' files if they exist in the pods as well ?Allinclusive
How do you exclude swift files also? Anyone?Coad
I
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.

Inconsonant answered 26/6, 2019 at 1:29 Comment(2)
Man U are Legend 10+Leper
on Xcode 15.4 this option disappear, anyone know how can I set it?Perloff
T
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

Tito answered 6/5, 2018 at 23:56 Comment(1)
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'Adlay
C
6
  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.
Chesna answered 27/1, 2017 at 21:47 Comment(1)
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.Casket
I
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
Ivy answered 28/7, 2017 at 9:32 Comment(1)
You could also just add next if target.name == 'MyPod' as the 3rd line of the answer @tung-fam gave.Nereid
D
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
Digged answered 12/8, 2020 at 10:19 Comment(0)
A
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
Analyze answered 16/11, 2023 at 6:49 Comment(0)
G
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:

Golconda answered 5/6 at 14:16 Comment(2)
This seems to present exactly the same solution as this 5 year old answer with minor UI changes.Forthwith
@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.Golconda

© 2022 - 2024 — McMap. All rights reserved.