如何从Xcode中的代码覆盖范围中排除Pod

问题描述 投票:21回答:4

有没有办法从代码覆盖范围中排除Pods? 我想看看Code Coverage仅针对我编写的代码。

不是它应该重要,但我正在使用Xcode 8。

ios xcode testing
4个回答
28
投票

这些步骤将有助于:

1.将这些行添加到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.运行pod install

现在你不会在测试覆盖范围内看到pods。

注意:它只排除了Objective-c pod而不是Swift


9
投票

要禁用swift代码的覆盖,可以使用SWIFT_EXEC的包装器(到目前为止我使用Xcode 9.3验证了这一点)。因此,完整的解决方案(包括Swift)将如下:

附加到您的Podfile(之后调用pod install):

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

将以下脚本(将其命名为SWIFT_EXEC-no-coverage)放在源树的根目录(必要时为chmod + x):

#! /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);

这是相应要点的链接:https://gist.github.com/grigorye/f6dfaa9f7bd9dbb192fe25a6cdb419d4


6
投票
  1. 单击左侧Project Navigator中的Pods项目
  2. 在右侧,打开项目和目标列表(如果尚未打开);然后单击Pods项目名称(不是目标)。
  3. 单击构建设置。
  4. 在搜索栏中,搜索“CLANG_ENABLE_CODE_COVERAGE”。
  5. 将“启用代码覆盖率支持”更改为NO。
  6. 重新运行测试。

2
投票

如果您正在开发一个pod并希望仅为您的代码覆盖:

    # 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
© www.soinside.com 2019 - 2024. All rights reserved.