Continuous integration using Jenkins for iOS testing

Continuous integration is a way of work, initially by Martin Fowler which consists of automatic integration of a project as often as possible in order to detect errors as soon as possible. It is possible to achieve continuous integration using Jenkins. This post it is about a practical example to run automatic tests for an iOS application every time we want (whenever someone commit code into develop, every day at the same time… )

We are going to need two jobs in Jenkins to run the tests.
One job that compile the iOS project and another one that execute the tests using the just compiled project.

 

Continuous Integration using Jenkins

Job to compile iOS Project

This job’s name could be job-TEST.

Things to take into account:

  • Project need a scheme
  • Compiling it from xcode plugin for jenkins cause that later is not possible to install the .app via instruments, so it is better do it by command line using xcodebuild.
    It is important to add the correct parameters to xcodebuild, in order that it can be installed via instruments.

We can see an example here:

xcodebuild -project "projectName.xcodeproj" \
-scheme "schemeName" \
-sdk iphonesimulator7.0 \
-configuration Release \
SYMROOT="./build" \
DSTROOT="./build" \
install

 

In this job, after compile the project, we should archive .app as an artifact:

Artifacts in Jenkins

Then execute the second job automatically (it should be done when the second job is done)

continuous integration using jenkins

 

Job to run the tests

Let’s call this job, for instance job-TEST-instrument.

Before doing this jobs, test should be done. This articles could help you: iOS Testing using Automation of Instruments.

In this job, we will install .app in a simulator, and then we will run the tests using Instruments.

It is possible to just include in a job the command to run a test from command line and then see the result of the job, to check if the test has ended correctly or not.
The problem is that you will need as many jobs as test you would like to run.

Jasmine is useful to avoid that by:

  • adding all the test cases that will be run
  • saving the result of the test in an xml file. This kind of file are easily manage in jenkins using the plugin jUnit

What is jasmine?

Jasmine is a behavior-driven development framework for testing JavaScript code. As test for iOS are in javaScript, it’s possible to use this framework.

Using jasmine you could have a testsuite and inside it, all the cases that are needed. And you can have all the suitcases needed. This library is prepare to execute test on ios using jasmine.

  • runTest.sh: the command to run instruments by command line is in this file. Check that the path where instruments is is correct.
  • Inside specs folder, you should include the test.js (with this name or another one) witch contain the testcases.
  • In ci.js you should indicate the path and the name of the javaScript file which has the test cases, for instance test.’s
  • When the tests finish, the test-result.xml file will be inside test-report folder. This file will have the result of the test.

Test.js

In test.js, we should include the tests that were generated with instruments.
It is quite simple.

describe(“TestSuite1”, function() {
         it(“test1”, function() {
                 ...
         });
         it(“test2”, function() {
                ...
         });
         ...
});

In each “it”, it should be included the code generated by instruments.
In instruments, as you remember, some code are added to validate if the test is finished correctly. Beside that, you should include some command of jasmine to validate that everything is correct

If we have code like this

if(target.frontMostApp().navigationBar().isValid() ){

this should be add above

expect(target.frontMostApp().navigationBar().isValid()).toBeTruthy();

Besides toBeTruthy(), there are more options to validate

  • toBeEqual()
  • toContain()
  • toBeNull()

There is a not. Example: expect(x).not.toEqual(y)

Once you modified your code and it is uploaded to the repository, In the job of Jenkins we should get the artifact  in the job

continuous integration using jenkins

Then move where the script is and execute this command to install the app and run the tests

./runTests.sh ci.js "/Users/job/aplication.app"

Afterwards, we add that once they are finished, check the result using jUnit plugin

continuous integration using jenkins

Then when the job finishes, it is posible to see the results.

When some of the fails. It neves happens!

continuous integration using jenkins
And when all has passed

continuous integration using jenkins

Are you going to integrate in Jenkins your automated tests?

Leave a Comment

¿Necesitas una estimación?

Calcula ahora