Wednesday, June 17, 2015

Objective-C: Travis-CI and Coveralls.io (1/2)

Just recently one of my projects has been chosen to become featured with test during the CocoaPods Test Jam.

Goal of this event was to add tests (XCTestSpecta / Expecta , Kiwi or Cedar) to the projects (THSegmentedPager in my case) to reach a certain coverage and add the project to the Travis-CI Continuous Integration service as well as to the Coveralls test coverage service.

In this article I'll skip the creation of the tests itself (Those can be seen in the repository and watching the Pull Request that he sent when everything was done) and jump directly to the Travis-CI and Coveralls.io integration.

Travis-CI

If your project is already on Github you could navigate to Travis-CI connect it with your Github account and enable it for Travis-CI. If not you'll need to do it at the end of the process and it will run with the next commit.

1) As the project uses CocoaPods he add a Gemfile with it as dependency which you can execute using bundler (sudo gem install bundler)
$ bundle install
Resolving dependencies...
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.7.0
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.1
Using claide 0.8.1
Using fuzzy_match 2.0.4
Using nap 0.8.0
Using cocoapods-core 0.37.2
Using cocoapods-downloader 0.9.0
Using cocoapods-plugins 0.4.2
Using netrc 0.7.8
Using cocoapods-trunk 0.6.1
Using cocoapods-try 0.4.5
Using colored 1.2
Using escape 0.0.4
Using molinillo 0.2.3
Using xcodeproj 0.24.2
Using cocoapods 0.37.2
Using bundler 1.10.3
Bundle complete! 1 Gemfile dependency, 21 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

2) Now he add the .travis.yml file which will tell to Travis what is exactly to do.
language: objective-c
before_script:
- bundle exec pod install --project-directory=THSegmentedPagerExample
script:
- set -o pipefail && bundle exec rake test | bundle exec xcpretty --test --color
- bundle exec pod lib lint --quick
To make this commands work we'll need to add the missing commands (rake and xcpretty)to the Gemfile and execute it again
source 'https://rubygems.org'
gem 'cocoapods'
gem 'rake'
gem 'xcpretty'
The script in .travis.yml points to a rake target named "test" which he had to define in the Rakefile
desc 'Run tests'
task :test do
  command = "xcodebuild \
    -workspace THSegmentedPagerExample/THSegmentedPagerExample.xcworkspace \
    -scheme THSegmentedPagerExample \
    -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6,OS=8.1' \
    test"
  system(command) or exit 1
end
task :default => :test
If you're not sure about how to write the script it is probably the best option to open a console; cd to the project root and run it

3) One more thing which is often forgotten is to share the scheme. This is done in Xcode

4) Now, as you'll probably want to show that you're using a CI service all that is left is to add a badge to the readme
[![Build Status](https://travis-ci.org/hons82/THSegmentedPager.png)](https://travis-ci.org/hons82/THSegmentedPager)

No comments:

Post a Comment

Golang setup PATH

Quite recently we startet in the company to use and write some Go programs. I love Go. It's easy to learn, read and modify. One of the m...