Code Coverage report with travis-ci, coveralls and fastlane

Recently, I am creating a new swift framework called Jellyfish, and I wish to include unit test and attach badges in the github repo (it is all about badges!). Here is how I setup the project

fastlane Settings

In project folder create Gemfile

1
2
3
4
5
6
7
8
9
10
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# gem "rails"
gem 'fastlane'
gem 'xcov'
gem 'cocoapods'

Run the following commands

1
2
3
4
5
bundle install --path vendor/
bundle exec fastlane init

# Select manual
# and press enter for all questions

Edit fastlane/Fastfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
# https://docs.fastlane.tools/actions
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

platform :ios do
desc "Tests for iOS"
lane :unit_test do
scan(scheme: "Jellyfish-iOS") # Change to your schema
xcov(
scheme: "Jellyfish-iOS", # Change to your schema
output_directory: "xcov_output",
ignore_file_path: ".xcovignore"
)
end
end

platform :macos do
desc "Tests for macOS"
lane :unit_test do
scan(scheme: "Jellyfish-macOS") # Change to your schema
xcov(
scheme: "Jellyfish-macOS", # Change to your schema
output_directory: "xcov_output",
ignore_file_path: ".xcovignore"
)
end
end

You can try to test on local machine with

1
bundle exec ios unit_test

Travis-CI Settings

Go to Travis-CI and enable ci for your repo

In project folder, add .travis.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
language: swift
osx_image: xcode9.1

cache:
- bundler

before_install:
- bundle install
- bundle update

script:
- bundle exec fastlane ios unit_test
- bundle exec fastlane macos unit_test

Push the code, and see it run on travis-ci!

Coveralls Settings

Go to Coveralls and enable code coverage for your repo.

Copy service_token on the page they redirect you to.

Go back to Travis-CI and in your project, select More Options -> Settings

Add an environment variable COVERALLS_REPO_TOKEN and the value is what you copy in Coveralls

Re-run the pipeline and wait for coveralls to update the code coverage percentage (some takes 8 hours for it to update)

Share Comments