Skip to content

Get Started: Visual Studio Code

Peter Thomas edited this page Jun 6, 2023 · 19 revisions

Video

A video walk-through of installing Visual Studio Code, the official Karate extension, and running your first API test - can be viewed here: Karate Kick Start - From Zero to First API Test in VS Code

Install Visual Studio Code

First, you will need Visual Studio Code installed. Open it before going to the next step.

Note that there is an even easier way to run your first Karate test using GitHub Codespaces, and you don't need to install anything.

Install Extension

The official VS Code Extension from Karate Labs can be found here: Karate. To easily install it from within VS Code, click on the "Plugins" icon in the activity-bar and search for "karate".

Make sure you choose the one from Karate Labs as shown below. It will have a verified "blue tick" and the logo is in the shape of a circle.

Create Project Folder

Open a new project and create a folder if required. This is where you can place your Karate tests. Tests are in files with a *.feature extension. Here below, we created a project folder called karate-demo and we are now creating a file called httpbin.feature.

image

Create First API Call

Cut and paste the following text into the new file you created. This is a simple HTTP POST call to a URL at https://httpbin.org.

Feature:

Scenario:
  * url 'https://httpbin.org/anything'
  * request { myKey: 'myValue' }
  * method post
image

Run Karate Script

Now click on the Run "codelens" to run the test:

image

View HTML Report

The "Output" tab should be visible with the log of all the HTTP calls and test results. Click on the link as shown below to open the HTML report.

image

Add an Assertion

Assertions are the most important part of any test and especially API tests ! Karate makes validating JSON payloads super easy.

Add the following 2 lines to the test script, you can cut-and-paste from the text below. This is an easy to read check if the HTTP status code in the response is 200 and whether the JSON returned in the API response contains the data expected.

  * status 200
  * match response.json == { myKey: 'myValue' }  
image

Get an Assertion to Fail

You can re-run the test and make sure the assertions pass. Change the 200 or the myValue to see what happens if an assertion fails. Refresh the HTML report in the browser to see what a failure looks like in the HTML report.

Do More

You can now refer to the documentation and examples for more complex tests !

Ask Questions

The best place to ask questions on how to use Karate or if you get stuck is the Karate "tag" on Stack Overflow.