Tutorial: Invoking test cases from Jenkins with JUnit result format
Before you begin
You must have the Automated API behavior testing application enabled and have created and published at least one test. You must have a Jenkins instance that you can create a new build in with the JUnit Plugin installed
About this tutorial
In this tutorial, you invoke a test case using injected variables from a jenkins pipeline
This tutorial takes you through the following steps
- Creating an API Hook
- Creating an API Key and Secret
- Creating a Jenkins build that invokes the API Hook
- Executing the build and reviewing the test results
Creating an API Hook
You can generate an API hook for each of the Test Suites within your Test Organization. API hooks are scoped to only one test suite and thus will only have access to operate on the data within that test suite; that is, if an API Hook is generated
for Test Suite 1
, it grants access only to the tests within Test Suite 1
. If you wish to interact with tests in multiple test suites, you will need to generate an API hook for each Test Suite.
How to create an API Hook
- Navigate to the Test Organization settings page by clicking the Cog icon in the menu bar.
- Within the Test Organization settings page, click the Hooks option under the API Hooks heading in the navigation menu.
- Now click on the + API Hook button to begin creating a new API Hook.
- Because API Hooks are scoped at a Test Suite level, you will need to select the test suite you wish to generate the hook for. Choose the appropiate test suite from the dropdown and optionally add a description for the API Hook. Click the
confirm icon to generate and save the API Hook.
- The API Hook URL has been created for your Test Suite. Make a note of this this URL as this will form the base path of all future API requests you make for the test suite.
Creating an API Key and Secret
API Keys and Secrets are the credentials required to use an API Hook. For a request to an API Hook to be authorised, both the X-API-Key
and X-API-Secret
headers must be provided. The
values of the API Key and Secret represent the values of these headers respectively.
- First navigate to the Test Organization settings page by clicking the Cog icon in the menu bar.
- Within the Test Organization settings page, click the KEYS option under the API Hooks heading in the navigation menu.
- Now click on the + API Key button to create a new API Key.
- You will then be shown the generated Key and Secret. Make a note of both of these, ensure the Secret is noted as it cannot be retrieved in the future. Give your new API Key a name and then
click the confirm icon to save it.
Creating a Jenkins build that invokes the API Hook
-
Create a Jenkins Project or navigate to an existing one
-
Select
Configure
from the the options
-
Under
Build
, clickAdd build step
dropdown and selectExecute Shell
-
Within the
Execute shell
block paste acurl
command that will invoke the hook that was created in step 1curl -XPOST -H 'x-api-key: <API-KEY>' -H 'x-api-secret: <API-SECRET>' -H "Content-type: application/json" -d '{ options: { JUnitFormat: true }}' '<API-HOOK-URL>/tests/run' >| testReports.xml
-
Under
Post-build Actions
, clickAdd post-build action
and in the dropdown selectPublish JUnit test results report
.- For
Test report XMLs
provide the name of the report created from the curl, in this tutorialtestReports.xml
- For
-
Save the project
Executing the build and reviewing the test results
- Click
Build Now
on the project
- Once the build has completed, you will now see a chart showing
- Click
Latest Test results
and review the Junit results
To expand upon this tutorial additional options and steps can be found under Using API Hooks
What you did in this tutorial
In this tutorial you completed the following activities:
- Created a API Hook
- Created a Key and Secret used for authenicating with a hook
- Invoked the API Hook using the Key and Secret to start a test and process the results