sobota 6. februára 2016

Robot Framework & Jenkins & Testlink Integration

A long time ago I was searching for way how to integrate robot framework and testlink. All I found was this manual from creator of TestLink plugin for Jenkins (Testlink Plugin).


This manual can be found here.

Summary


I decided that I will prepare manual on my own to go through all integration and make it easy to achieve.

I'm going to make it step by step from the beginning. I assume that you have Jenkins and TestLink working.

For this tutorial I will work with following setup:
1) Jenkins is running on 192.168.1.32:8011
2) TestLink is running on 192.168.1.32:8012/testlink
Versions:
1) Jenkins - 1.642.1
2) TestLink - 1.9.14
3) TestLink Plugin - 3.11

At the end we will have working integration using clean project.
The trick at the end is to have custom field in TestLink and name of robot testcase the same.

Note:
If you are familiar with docker you can download my setup (used for running of this integration) from https://github.com/alans09/testlink-robot-jenkins
Just run:
- docker build -t local/jenkins -f docker-jenkins .
- docker build -t local/testlink -f docker-testlink .

after this you can start containers:
- docker run -d -p 8011:8080 local/jenkins
- docker tun -d -p 8012:80 local/testlink

You will get clean installation where you can start step by step using this guide.
Login to TestLink is admin/admin.



So let's do this step by step.

TestLink configuration.

I'm going to start from clean TestLink installation with default username/password to show how to create/manage project that will be used for Jenkins integration.

We need to create new project.
Create TestLink Project
Create a new project

Project settings:

Name: TestProject
Prefix: tp
Description: <we don't need any - feel free to add>
Enhanced features: only required is Test Automation (API keys) - I'm leaving it as it is. (with enabled Testing Priority and Test Automation (API keys))
Availability: Active and Public enabled

Create project with this settings using button "Create".

Created project


Using desktop icon navigate to TestLink desktop.

Now we need to define own custom fields that will be used for TestLink plugin to upload results to.

Click on the "Define Custom Fields" under "System". Create new using "Create" and name it "robot" (This name is on you. I'm using this one to make integration work)

Custom field settings:

Name: robot
Label: robot
Available for: Test Case
Type: string
Enable on: Test Spec Design
Display on test execution: yes

Click on "Add and assign (to current test project)".

Created custom field
Using desktop icon navigate to TestLink desktop.

We need also to create Test Plan to be used. Click on "Test Plan Management" and click "Create" to create new Test Plan for our TestProject.

Test Plan settings:

Name: TestPlan1
Description: <not needed - feel free to fill in>
Active: True
Public: True

Click on "Create".
Created test plan
Using desktop icon navigate to TestLink desktop.

Now we have all ready to create set of Test Cases to be used for our integration.

Click on "Test Specification" and create for example 2 Test Cases.

Click on "Create Test Suite"


Test Suite settings:
Test Suite Name: Test-Suite-1

We don't need anything else for this. Click on "Save". There will be Test-Suite-1 created under TestProject.
We're going to create a few Test Cases now.

Click on Test-Suite-1 on the treeview and click on "Create Test Case".

Test Cases properties:

(We will create similar cases and changes will be in title and robot (custom field))
Test Case Title:  Test-Case-01, Test-Case-02
Status: Final
Execution type: Automated
robot: test-case-01, test-case-02

Field "robot" is important. This is field to match our results from robot framework.

Click on "Create" and repeat process for 2nd Test Case.

Final Test Suite
At the end we have following structure that is visible in Test Specification.

TestProject (2)
- Test-Suite-1 (2)
- - tp-1: Test-Case-01
- - tp-2: Test-Case-02

















Now we just need add this cases to our Test Plan using "Add/Remove Test Cases" from dashboard. Click on it select our Test Cases and click "Add selected". Test Cases will be yellow and this means they are added to Test Plan.
Added test cases to Test Plan
Last thing we need is to enable automation from the "My settings" menu.
Click on "My Settings" and under "API interface" create new personal API access key. Click on "Generate a new key" and copy this key. (It's needed in Jenkins.

First part is done. We have TestLink prepared to integrate with Robot Framework and Jenkins. Let's create Jenkins/Robot part.


Jenkins configuration.

This is the easy part of setup.


First of all you need to have installed TestLink plugin. This can be achieved by visiting "Manage Jenkins -> Manage Plugins -> Available [tab] and install TestLink Plugin

This is all we need for this step.

We need to configure the TestLink setup. Navigate to "Manage Jenkins -> Configure System" and click on Testlink Installation button "Add".

Testlink settings:

Name: integration (this could be anything)
URL: http://192.168.1.32:8012/testlink/lib/api/xmlrpc/v1/xmlrpc.php
Developer Key: <key generated at the end of TestLink part>

Click on "Save"

Last thing we need is to create job.
Click on "New Item" and select "Freestyle project". Name it "integration-test" (or whatever you need)

Now we need to create .robot files (Test Cases)
This step is completely optional if you have your own tests.

1) Add build step -> execute shell or execute windows batch command (depending on machine you are working on)
For me this is execute shell.
I'm going to create robot framework test suite. (You can download it from git or upload to server etc.)

rm -rf run.robot || True
echo "*** Test Cases ***" > run.robot
echo "test-case-01" >> run.robot
echo "   Log    Test 01" >> run.robot
echo "test-case-02" >> run.robot
echo "   Log    Test 02" >> run.robot

This step only runs our created run.robot robot file.
2) Add build step -> execute shell or execute windows batch command

pybot -x junit.xml run.robot
# this will run run.robot file and creates also junit.xml as junit result file.

3) Add post build action -> Publish JUnit test result report
Add junit.xml to Test Report XMLs. Do not change anything else.

Click on "Save" and try to run our job.
We should have nice passed run as shown on picture below.
Successful run

The magic begins now. Let's edit our job and add 1 step.
4) Add build step -> Invoke TestLink

TestLink Configuration settings:

TestLink Version: integration
Test Project Name:  TestProject
Test Plan Name: TestPlan1
Build: ${BUILD_ID}  #this will add new Build to TestPlan1 with name that is ${BUILD_ID} variable
Custom Fields: robot

Under "Result Seeking Strategy" add "Test Result Seeking Strategies" and click on "Add strategy".
Select "JUnit case name" and set as follows:
Include Patter: junit.xml
Key Custom Field: robot

Click on "Save" and run our job.

Result should look like this:
Successful run of Test Link integration



Important notice:

Be sure to check junit.xml and check 'name' field of xml result. This field have to be EXACT the same as field 'robot' in TestLink.