Using Assertible and CloudBees CodeShip for API testing

3 minute read

About Assertible

Assertible is an API testing and monitoring tool that can be used with continuous integration and delivery services like CodeShip to test and validate your web applications.

By using Assertible you can ship more reliable code for your teams and your customers.

The Assertible documentation provides a great guide to getting started, and the instructions below have more information on integrating with CodeShip to Triggering tests during a build and Running tests after a deployment.

CloudBees CodeShip Pro

Setting your API token

To run your Assertible API tests on CodeShip, you will need to add two values to your encrypted environment variables that you encrypt and include in your codeship-services.yml file:

  • ASSERTIBLE_API_TOKEN

  • ASSERTIBLE_SERVICE_ID

You can get these values out of your Assertible dashboard after setting up a new web service.

Triggering tests during a build

To test your API or web app during your CI build, Assertible recommends building and running your app on CodeShip, and then using an ngrok tunnel to trigger the test suite.

To do this, add the following script in your repository, that you will then call from your codeship-steps.yml file:

- name: Assertible service: app command: assertible.sh

Inside of the assertible.sh file, use the following code:

# Start your application (NOTE: CUSTOMIZE THIS COMMAND) node server.js & # download and install ngrok curl -s https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip > ngrok.zip unzip ngrok.zip ./ngrok http 5000 > /dev/null & # sleep to allow ngrok to initialize sleep 2 # Download JSON parser for determining ngrok tunnel wget https://stedolan.github.io/jq/download/linux64/jq chmod +x jq # extract the ngrok url NGROK_URL=$(curl -s localhost:4040/api/tunnels/command_line | jq --raw-output .public_url) curl -s $TRIGGER_URL -d'{ "environment": "'"$CI_BRANCH-$CI_COMMIT_ID"'", "url": "'"$NGROK_URL"'", "wait": true }'

And that’s it! Be sure to customize the command that starts your application. Now when your build runs, your application will be started and Assertible will run the API tests you’ve configured.

Running tests after a deployment

To run tests against your API or website after a deployment, add the following command to a script placed in your repository, that you will then call from your codeship-steps.yml file:

# POST a new deployment to Assertible, and your tests will run against it curl -u $ASSERTIBLE_API_TOKEN: -XPOST "https://assertible.com/deployments" -d'{\ "service": "'"${ASSERTIBLE_SERVICE_ID}"'",\ "environmentName": "staging",\ "version": "'"${CI_COMMIT_ID}"'"\ }'

Call this script on all deployment-related branches by specifying the tag. Be sure to add this step after your deployment, so that the tests are run against the new version of your application. For example:

- name: deploy service: app tag: master command: your deployment commands - name: assertible service: app tag: master command: deploy-assertible.sh

CloudBees CodeShip Basic

Setting your API token

To run your API tests with Assertible, you will need to add two values to your CodeShip project’s environment variables:

  • ASSERTIBLE_API_TOKEN

  • ASSERTIBLE_SERVICE_ID

You can get these values out of your Assertible dashboard after setting up a new web service.

Triggering tests during a build

To test your API or web app during your CI build, Assertible recommends building and running your app on CodeShip, and then using an ngrok tunnel to trigger the test suite.

To do this, add the following code to a script in your repository and run it in your setup commands:

# Start your application (NOTE: CUSTOMIZE THIS COMMAND) node server.js & # download and install ngrok curl -s https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip > ngrok.zip unzip ngrok.zip ./ngrok http 5000 > /dev/null & # sleep to allow ngrok to initialize sleep 2 # Download JSON parser for determining ngrok tunnel wget https://stedolan.github.io/jq/download/linux64/jq chmod +x jq # extract the ngrok url NGROK_URL=$(curl -s localhost:4040/api/tunnels/command_line | jq --raw-output .public_url) curl -s $TRIGGER_URL -d'{ "environment": "'"$CI_BRANCH-$CI_COMMIT_ID"'", "url": "'"$NGROK_URL"'", "wait": true }'

And that’s it! Be sure to customize the command that starts your application. Now when your build runs, your application will be started and Assertible will run the API tests you’ve configured.

Running tests after a deployment

To run tests against your API or website after a deployment, add a new custom-script step to your CodeShip project’s deployment pipelines.

The custom-script step will call the Assertible Deployments API to track the new release and run tests against the newly deployed version of your app by using the following command:

# POST a deployment release to Assertible, and your tests will run against it curl -u $ASSERTIBLE_API_TOKEN: -XPOST "https://assertible.com/deployments" -d'{\ "service": "'"${ASSERTIBLE_SERVICE_ID}"'",\ "environmentName": "staging",\ "version": "'"${CI_COMMIT_ID}"'"\ }'

For a more complete walk-through of setting this up read this CodeShip blog post: Add post-deploy smoke tests to any CodeShip pipeline.