A part of an engineer's toolbox is a good automation workflow. One of the tools in that box is a Continuous Integration provider, which can help teams prevent integration problems and improve quality control.
Runscope can be used by itself, running and monitoring APIs, but another use case is combining it with your CI workflow. Our CEO, John Sheehan, created a sample Python script that can be used to trigger a set of tests in your Runscope account, and change your application build status based on its results.
In this tutorial, we're going to show you how to use the sample script with Codeship. We'll cover how to:
- Generate a Runscope API access token
- Get your API tests Trigger URL
- Set up your environment variables in Codeship
- Run the Python script as part of your CI test commands
We have also included a section at the end for Codeship Pro users. :)
Let's get started!
Setting up the Python script
You can find the script sample in our GitHub organization:
The two files we're interested are "requirements.txt" and "app.py". For this tutorial, I'm just going to work with the raw links from our GitHub repository.
If you're integrating this in your project, I highly recommend either forking this to your own repository, or adding these files to a separate folder. That way, you can prevent your build from breaking in case there's an update to the repository.
Getting your Runscope variables
When running this script from the command line, you can pass one parameter to it, which is a Runscope Trigger URL. If you want to run a single set of tests, you can find the tests trigger URL under your environment settings:
If you want to run all the test in a bucket, you can find the trigger URL in your bucket settings:
Generating Your Runscope API Key
We need a Runscope personal access token to interact with the Runscope API and retrieve the results from our test run.
To get your personal token, head over to https://www.runscope.com/applications, and create a new application. You can just use dummy URLs for the app and callback URL values (e.g. http://example.org).
Scroll down to the bottom of the page, and copy the personal access token value. We're going to use that in the next step.
Integrating with Codeship
In your Codeship account, select the project you're working with and click on Project Settings -> Environment Variables.
We only need to add one key here named RUNSCOPE_ACCESS_TOKEN. Paste the value that you copied in our previous step, and click on "Save Configuration".
Now, select "Test" on the left-hand "Project Settings" menu.
The Codeship environment already comes with python and pip pre-installed. The first thing we need to do is make sure the necessary packages for the script are installed. Add the following command to your "Setup Commands" window:
pip install -r https://raw.githubusercontent.com/Runscope/python-trigger-sample/master/requirements.txt
Note: Remember to change the requirements URL to your fork or local file.
Next, I'm going to add another command just below it to download our `app.py` file (you can skip this step if you copied the files to your project):
Finally, under the Configure Test Pipelines header, in the "Test Commands" window, we can run our script. It takes one parameter, which is the Trigger URL you copied at the beginning of this tutorial. So we can just run the command as:
python app.py https://api.runscope.com/radar/your_test_trigger_id/trigger?runscope_environment=your_runscope_environment_id
Note: Make sure to change the URL after `app.py` to your tests Trigger URL.
Checking Your Build Runs
In your next build runs, you should be able to see a new step running the Python script, and hopefully returning a green checkmark ✅:
Integrating with Codeship Pro
Codeship Pro is a more advanced offering from Codeship. It offers you more control and customization over your CI process, and includes native Docker support.
If you're using the Pro plan and want to integrate your workflow with Runscope API tests, we created a sample project that you can find here:
It's based on Codeship's tutorial project, and it includes instructions on everything you need to set it up. When you run it, it will spin up a separate container to run your Runscope API tests, in parallel to the sample Ruby application.
Continuous Integration Complete
With Runscope integrated into your CI process, we hope that you have even more confidence in your builds and that your APIs will be 200 OK.
We used Codeship in this tutorial, but these instructions should also apply to other CI providers like CircleCI or Jenkins (we have a Jenkins plugin). If you need any help with those, please reach out to our awesome support team.