Fengson
Fengson

Reputation: 4912

How to exclude Pods from Code Coverage in Xcode

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.

Upvotes: 29

Views: 10770

Answers (8)

William James
William James

Reputation: 1

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:

Upvotes: 0

Hassan Riaz
Hassan Riaz

Reputation: 106

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

Upvotes: 0

schmidt9
schmidt9

Reputation: 4528

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

Upvotes: 0

jacob_g
jacob_g

Reputation: 779

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.

Upvotes: 23

Grigory Entin
Grigory Entin

Reputation: 1837

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

Upvotes: 10

Daniel Suia
Daniel Suia

Reputation: 31

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

Upvotes: 3

Tung Fam
Tung Fam

Reputation: 8147

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

Upvotes: 36

SeaJelly
SeaJelly

Reputation: 1746

  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.

Upvotes: 5

Related Questions