Code coverage for Xcode projects (Objective-C only)

Overview

XcodeCoverage

CocoaPods Version

XcodeCoverage provides a simple way to generate reports of the Objective-C code coverage of your Xcode project. Generated reports include HTML and Cobertura XML.

Coverage data excludes Apple's SDKs, and the exclusion rules can be customized.

Sadly, Swift coverage is not supported.

Installation: Standard

Use the standard installation if you want to customize XcodeCoverage to exclude certain files and directories, such as third-party libraries. Otherwise, the CocoaPods installation described below may be more convenient.

  1. Fork this repository.
  2. Place the XcodeCoverage folder in the same folder as your Xcode project.
  3. In your main target's Build Phases, add a Run Script build phase to execute XcodeCoverage/exportenv.sh

A few people have been tripped up by the last step: Make sure you add the script to your main target (your app or library), not your test target.

Installation: CocoaPods

A CocoaPod has been added for convenient use in simple projects. There are a couple of things you should be aware of if you are using the CocoaPod instead of the standard method:

  • There will be no actual files added to your project. Files are only added through preserve_paths, so they will be available in your Pods/XcodeCoverage path, but you will not see them in Xcode, and they will not be compiled by Xcode.
  • You will not be able to modify the scripts without those modifications being potentially overwritten by CocoaPods.

If those caveats are deal-breakers, please use the standard installation method above.

The steps to install via CocoaPods:

  1. Add pod 'XcodeCoverage', '~>1.0' (or whatever version specification you desire) to your Podfile.
  2. Run pod install. This will download the necessary files.
  3. In your main target, add a Run Script build phase to execute Pods/XcodeCoverage/exportenv.sh.

Again, make sure you add the script to your main target (your app or library), not your test target.

Xcode Project Setup

XcodeCoverage comes with an xcconfig file with the build settings required to instrument your code for coverage analysis.

If you already use an xcconfig, include it in the configuration you want to instrument:

  • Standard installation: #include "XcodeCoverage/XcodeCoverage.xcconfig"
  • CocoaPods installation: #include "Pods/XcodeCoverage/XcodeCoverage.xcconfig"

If you don't already use an xcconfig, drag XcodeCoverage.xcconfig into your project. Where it prompts "Add to targets," deselect all targets. (Otherwise, it will be included in the bundle.) Then click on your project in Xcode's Navigator pane, and select the Info tab. For the configuration you want to instrument, select XcodeCoverage.

If you'd rather specify the build settings by hand, enable these two settings at the project level:

  • Instrument Program Flow
  • Generate Legacy Test Coverage Files

Make sure not to instrument your AppStore release.

Execution

  1. Run your unit tests.
  2. In Terminal, execute getcov in your project's XcodeCoverage folder.

getcov has the following command-line options:

  • --show or -s: Show HTML report.
  • --xml or -x: Generate Cobertura XML.
  • -o output_dir: Specify output directory.
  • -i info_file: Specify name of generated lcov info file.
  • -v: Enable verbose output.
  • -h or --help: Show usage.

If you make changes to your test code without changing the production code and want a clean slate, use the cleancov script.

If you make changes to your production code, you should clear out all build artifacts before measuring code coverage again. "Clean Build Folder" by holding down the Option key in Xcode's "Product" menu, or by using the ⌥⇧⌘K key combination.

Optional: XcodeCoverage can prompt to run code coverage after running unit tests:

  • Edit Xcode scheme -> Test -> Post-actions
  • Set "Shell" to: /bin/bash
  • Set "Provide build settings from" to your main target
  • Set script to source XcodeCoverage/run_code_coverage_post.sh for standard installation. For CocoaPods installation, use source Pods/XcodeCoverage/run_code_coverage_post.sh

Excluding Files From Coverage

If there are files or folders which you want to have the coverage generator ignore (for instance, third-party libraries not installed via CocoaPods or machine-generated files), add an .xcodecoverageignore file to your SRCROOT.

Each line should be a different file or group of files which should be excluded for code coverage purposes. You can use SRCROOT relative paths as well as the * character to indicate everything below a certain directory should be excluded.

Example contents of an .xcodecoverageignore file:

${SRCROOT}/TestedProject/Machine Files/*
${SRCROOT}/TestedProject/Third-Party/SingleFile.m
${SRCROOT}/TestedProject/Categories/UIImage+IgnoreMe.{h,m}

Note: If you were using a version of XcodeCoverage prior to 1.3, you will need to move the list of files and folders you wish to ignore to the .xcodecoverageignore file. The current setup will prevent your customized list from being overwritten when there is an update to this project.

Credits

The lcov -> Cobertura script is from https://github.com/eriwen/lcov-to-cobertura-xml/ and is bound by the license of that project.

Comments
  • XCode 7 is not supported

    XCode 7 is not supported

    When running on XCode 7 I get:

    lcov: ERROR: no valid records found in tracefile Coverage.info Reading data file Coverage.info genhtml: ERROR: no valid records found in tracefile Coverage.info Coverage report is in lcov folder

    opened by konrad-g 17
  • Cobertura-formatted XML reports?

    Cobertura-formatted XML reports?

    First off - I just ran through your setup and have to say that this is phenomenal. By far, the most easiest and quickest setup for measuring code coverage of iOS projects. I was nervous when I found an outdated blog post referencing this project, but then when I came to GitHub and saw you are actively maintaining it, I was pumped. Thank you

    How can I modify this script to generate an XML report that may be consumed by Cobertura in order to better display the coverage stats in Jenkins?

    opened by obuseme 8
  • Unable to generate code coverage with Xcode 10 Beta

    Unable to generate code coverage with Xcode 10 Beta

    HI,

    I am generating Code coverage using terminal after test cases succeed by running run_code_coverage_post.sh. It works fine with Xcode 10.4.1, but on Xcode 10 beta it's giving below errors: Please help me in resolving these this issue.

    geninfo: ERROR: cannot read /Users/Abhishek/Library/Developer/Xcode/DerivedData/TRADFRI-fuzpwmjhzkdzrbbfjbnlwglkcolz/Build/Intermediates.noindex/TRADFRI_LIB.build/Debug-iphonesimulator/TRADFRI_LIBTests.build/Objects-normal/undefined_arch! Reading tracefile Coverage.info lcov: ERROR: cannot read file Coverage.info! Reading tracefile Coverage.info lcov: ERROR: cannot read file Coverage.info! cat: /Users/Abhishek/Documents/Usman/LSProject/tradfri-ios/External/FREKVENS_LIB/.xcodecoverageignore: No such file or directory Reading tracefile Coverage.info lcov: ERROR: cannot read file Coverage.info! Reading data file Coverage.info genhtml: ERROR: cannot read file Coverage.info! The file /Users/Abhishek/Library/Developer/Xcode/DerivedData/TRADFRI-fuzpwmjhzkdzrbbfjbnlwglkcolz/Build/Products/Debug-iphonesimulator/lcov/index.html does not exist.

    Thanks and Regards, Usman Ansari

    opened by usmaanirfan 5
  • Lcov 1.11 + CocoaPod + Cobertura

    Lcov 1.11 + CocoaPod + Cobertura

    This pull request is to add lcov 1.11 (addressing #24) as well as adding support for adding the tool via CocoaPods. I've updated the README to show the appropriate instructions - the TL;DR is it's less flexible, but WAY easier to get started.

    If you do want to add CocoaPods support, please tag your repo once this is merged in with 1.0.0 and then push up to the CocoaPods trunk.

    Please let me know if you have any questions whatsoever.

    opened by designatednerd 5
  • .gcda is not generated

    .gcda is not generated

    I don't know if it issue of XcodeCoverage or Xcode itself.

    I have target with name 'app develop'. If I run test I don't see .gcda files generated. If I run tests for 'app' target everything works.

    Except of renaming target are there solutions?

    opened by emartynov 4
  • How to  set the specified files to whitelist

    How to set the specified files to whitelist

    How to set the specified files to whitelist? i want to add some files to whitelist, and only Check these files。 our project is very big. and contain so many sdks, usually one feature we only chang A few files, i only want to check that files. don't care others files. What should I do? thanks, waitting for your replay!

    opened by ziyouzhe4 3
  • Can you retag and update the pod to include lcov1.12?

    Can you retag and update the pod to include lcov1.12?

    Hello Jon, It would be helpful if the Pod allowed you to install the latest lcov release, but the last available tag is stuck at lcov 1.11. Could you retag and update and publish the podspec?

    opened by Panajev 3
  • No unit test coverage, but AppDeledate coverage

    No unit test coverage, but AppDeledate coverage

    I installed XcodeCoverage to my project like descriped in README.md. I tried pods and standard installation and had always the same effect:

    • code coverage on every line which were executed by the app delegate before the unit tests started
    • no code coverage on lines which were executed by the unit tests

    I already asked Google but didn't get an answer that worked. I also tried the __gcov_flush() function, but no effect neither.

    I use Xcode6, iOS simulator 8.3 and 8.4.

    Any ideas?

    opened by shirerom 3
  • exportenv.sh always exports i386

    exportenv.sh always exports i386

    I just noticed an issue when trying to run my tests against a 64bit simulator e.g. an iPhone 6 simulator. Then the script always puts i386 as CURRENT_ARCH into the env.sh file. Might this be an issue I'm having with my build settings or does anyone know another way to fix this? Thanks!

    opened by lumaxis 3
  • Added command line parameters to getcov

    Added command line parameters to getcov

    Added parameters to let the caller specify the name of the INFO_FILE and location of the generated report without having to modify the scripts. This makes it easier to use the getcov script as is in CI.

    opened by taylesworth 3
  • Is XcodeCoverage going to be updated to work with the LLVM-COV profdata file?

    Is XcodeCoverage going to be updated to work with the LLVM-COV profdata file?

    Since SDK 9 I am unable to use XcodeCoverage.

    I cannot turn on INSTRUMENT_PROGRAM_FLOW to generate the GCOV files, as it causes my test run to fail. Lots of "cannot merge previous GCDA file: corrupt arc tag" entries in the console, and it then crashes in main.m showing BAD_ACCESS on the line calling UIApplicationMain.

    I cannot turn INSTRUMENT_PROGRAM_FLOW on for the main target, but without it, I cannot generate GCDA files.

    Is there a way to fix this, or do we need to wait till XcodeCoverage supports LLVM-COV profdata?

    opened by bixbarton 2
  • Unable to see coverage report in XCode 12.4 with new build system

    Unable to see coverage report in XCode 12.4 with new build system

    Unable to see coverage report in XCode 12.4 with new build system. Please see below error

    15:21:19 + mkdir -p ../../build/reports/coverage_Report 15:21:19 + echo 'XcodeCoverage report generating..' 15:21:19 XcodeCoverage report generating.. 15:21:19 + XcodeCoverage/getcov -o ../../build/reports/coverage_Report 15:21:19 output_dir = ../../build/reports/coverage_Report 15:21:19 /opt/jenkins/workspace/iOS_PullRequest_Unit/build/reports/coverage_Report /opt/jenkins/workspace/iOS_PullRequest_Unit/src/elektra 15:21:19 /opt/jenkins/workspace/iOS_PullRequest_Unit/src/elektra 15:21:19 Capturing coverage data from /Users/jenkins/Library/Developer/Xcode/DerivedData/elektra-dqgphexvdqtbmicihhexfohtaasc/Build/Intermediates.noindex/elektra.build/Debug_Fluke-iphonesimulator/elektra.build/Objects-normal/undefined_arch 15:21:19 Found LLVM gcov version 12.0.0, which emulates gcov version 4.2.0 15:21:19 geninfo: ERROR: cannot read /Users/jenkins/Library/Developer/Xcode/DerivedData/elektra-dqgphexvdqtbmicihhexfohtaasc/Build/Intermediates.noindex/elektra.build/Debug_Fluke-iphonesimulator/elektra.build/Objects-normal/undefined_arch! 15:21:19 lcov: ERROR: cannot read file Coverage.info! 15:21:19 Reading tracefile Coverage.info

    opened by lakshmijajula 0
  • Unable to see coverage report in XCode 11.6 with new build system.

    Unable to see coverage report in XCode 11.6 with new build system.

    Unable to see coverage report in XCode 11.6 with new build system. Please see below error

    11:00:18 + mkdir -p ../../build/reports/coverage_Report 11:00:18 + echo 'XcodeCoverage report generating..' 11:00:18 XcodeCoverage report generating.. 11:00:18 + XcodeCoverage/getcov -o ../../build/reports/coverage_Report 11:00:18 output_dir = ../../build/reports/coverage_Report 11:00:18 /opt/jenkins/workspace/iOS_PullRequest_Unit/build/reports/coverage_Report /opt/jenkins/workspace/iOS_PullRequest_Unit/src/elektra 11:00:18 /opt/jenkins/workspace/iOS_PullRequest_Unit/src/elektra 11:00:18 Capturing coverage data from /Users/jenkins/Library/Developer/Xcode/DerivedData/elektra-dqgphexvdqtbmicihhexfohtaasc/Build/Intermediates.noindex/elektra.build/Debug_Fluke-iphonesimulator/elektra.build/Objects-normal/undefined_arch 11:00:18 Found LLVM gcov version 11.0.3, which emulates gcov version 4.2.0 11:00:18 geninfo: ERROR: cannot read /Users/jenkins/Library/Developer/Xcode/DerivedData/elektra-dqgphexvdqtbmicihhexfohtaasc/Build/Intermediates.noindex/elektra.build/Debug_Fluke-iphonesimulator/elektra.build/Objects-normal/undefined_arch! 11:00:18 lcov: ERROR: cannot read file Coverage.info! 11:00:18 Reading tracefile Coverage.info

    opened by anuragfluke 0
Releases(v1.4.0)
  • v1.4.0(Jan 1, 2020)

  • v1.3.2(Feb 2, 2019)

  • v1.3.1(Sep 25, 2017)

  • v1.3.0(Mar 1, 2016)

    Version 1.3.0

    29 Feb 2016

    • Updated lcov to 1.12 for Xcode 7 compatibility. Thanks to: Nico Elayda
    • Instead of editing getcov directly to exclude certain files from coverage, specify them in an .xcodecoverageignore file in SRCROOT. Thanks to: Ellen Shapiro
    • Add ability to generate Clover XML reports for Bamboo CI. Call getcov with --xmlclover or -xc. Thanks to: Kamil Pyć
    Source code(tar.gz)
    Source code(zip)
  • v1.2.2(Mar 22, 2015)

    Version 1.2.2

    22 Mar 2015

    • Add getcov command line parameters, making it easier to use XcodeCoverage in continuous integration. Thanks to: Tom Aylesworth
    • Add Cobertura XML generation. Thanks to: Ellen Shapiro
    • Support use as CocoaPod. Thanks to: Ellen Shapiro
    • Update to lcov 1.11. Thanks to: Ellen Shapiro
    • Add XcodeCoverage.xcconfig for simple project setup.
    Source code(tar.gz)
    Source code(zip)
Owner
Jon Reid
Practicing test-driven development on Apple platforms since 2001.
Jon Reid
A Matcher Framework for Swift and Objective-C

Nimble Use Nimble to express the expected outcomes of Swift or Objective-C expressions. Inspired by Cedar. // Swift expect(1 + 1).to(equal(2)) expect(

Quick 4.6k Dec 31, 2022
The Swift (and Objective-C) testing framework.

Quick is a behavior-driven development framework for Swift and Objective-C. Inspired by RSpec, Specta, and Ginkgo. // Swift import Quick import Nimbl

Quick 9.6k Dec 31, 2022
A light-weight TDD / BDD framework for Objective-C & Cocoa

Specta A light-weight TDD / BDD framework for Objective-C. FEATURES An Objective-C RSpec-like BDD DSL Quick and easy set up Built on top of XCTest Exc

Specta / Expecta 2.3k Dec 20, 2022
Automatic testing of your Pull Requests on GitHub and BitBucket using Xcode Server. Keep your team productive and safe. Get up and running in minutes. @buildasaur

Buildasaur Automatic testing of your Pull Requests on GitHub and BitBucket using Xcode Server. Keep your team productive and safe. Get up and running

Buildasaurs 774 Dec 11, 2022
Implementing and testing In-App Purchases with StoreKit2 in Xcode 13, Swift 5.5 and iOS 15.

StoreHelper Demo Implementing and testing In-App Purchases with StoreKit2 in Xcode 13, Swift 5.5, iOS 15. See also In-App Purchases with Xcode 12 and

Russell Archer 192 Dec 17, 2022
Control your iPhone from inside Xcode for end-to-end testing.

Remote - Control your iPhone from Xcode "Remote" is a plugin for Xcode that allows you to control an iPhone from a window on your Mac during developme

John Holdsworth 791 Dec 26, 2022
Tool for generating Acceptance Tests in Xcode, inspired by Fitnesse

AcceptanceMark is a tool for generating Acceptance Tests in Xcode, inspired by Fitnesse. Read this blog post for a full introduction to AcceptanceMark

Andrea Bizzotto 64 Jun 18, 2022
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!

OHHTTPStubs OHHTTPStubs is a library designed to stub your network requests very easily. It can help you: test your apps with fake network data (stubb

Olivier Halligon 4.9k Dec 29, 2022
Mock Alamofire and URLSession requests without touching your code implementation

Mocker is a library written in Swift which makes it possible to mock data requests using a custom URLProtocol. Features Requirements Usage Activating

WeTransfer 898 Dec 26, 2022
This repository accompanies Test-Driven Development in Swift: Compile Better Code with XCTest and TDD

Apress Source Code This repository accompanies Test-Driven Development in Swift: Compile Better Code with XCTest and TDD by Gio Lodi (Apress, 2021). D

Apress 57 Jan 1, 2023
Take home task from one company developed on VIPER, UI Through code and splash from plist

HelloFresh Dev Team - Mobile iOS Developer Test Hello! Thank you for taking the time to try our iOS test. The goal of the test is to assess your codin

Jawad Ali 4 Aug 13, 2022
This repo holds the code for Dubizzle & Bayut test App

DubizzleClassified This repo holds the code for Dubizzle & Bayut test App About App This is a simple app which basically fetches item list from the gi

Ghassan 0 Jun 2, 2022
Lightweight touch visualization library in Swift. A single line of code and visualize your touches!

TouchVisualizer is a lightweight pure Swift implementation for visualising touches on the screen. Features Works with just a single line of code! Supp

Morita Naoki 851 Dec 17, 2022
TestSchedulerDemo - Demonstration code for iOS Unit Tests and Asynchronous

TestSchedulerDemo This repository contains demonstration code for my Medium arti

Carsten Wenderdel 0 Mar 19, 2022
Detailed explanations and implementations of various maths concepts for writing high performance code/algorithms backed with Unit tests.

Detailed explanations and implementations of various maths concepts which can help software Engineers write high performance code/algorithms backed with Unit tests.

Mussa Charles 2 Sep 25, 2022
The iOS pod which can collect profile data to detect code coverage.

CodeCoverageKit Installation CodeCoverageKit is available through CocoaPods.

木子 2 Sep 29, 2021
JSPatch bridge Objective-C and Javascript using the Objective-C runtime. You can call any Objective-C class and method in JavaScript by just including a small engine. JSPatch is generally used to hotfix iOS App.

JSPatch 中文介绍 | 文档 | JSPatch平台 请大家不要自行接入 JSPatch,统一接入 JSPatch 平台,让热修复在一个安全和可控的环境下使用。原因详见 这里 JSPatch bridges Objective-C and JavaScript using the Object

bang 11.4k Jan 1, 2023
⏲ A tiny package to measure code execution time. Only 20 lines of code.

Measure ⏲ A tiny package to measure code execution time. Measure.start("create-user") let user = User() Measure.finish("create-user") Console // ⏲ Mea

Mezhevikin Alexey 3 Oct 1, 2022
Enables you to hide ur UIViews and make them screen/screen shot proof. objective c/c++ only

SecureView Enables you to hide ur UIViews and make them screen/screen shot proof. objective c/c++ only Usage UIWindow* mainWindow; - (void) setup {

Red16 6 Oct 13, 2022
Weather-app - Created a simple weather app on Xcode using SwiftUI, only shows one location

weather-app Created a simple weather app on Xcode using SwiftUI, only shows one

Rahul Kadiyala 1 Feb 11, 2022