-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Get Started: Visual Studio Code
Note that there is an even easier way to run your first Karate test using GitHub Codespaces !
First, you will need Visual Studio Code installed.
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.
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
.
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
Now click on the Run
"codelens" to run the test:
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.
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' }
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.
You can now refer to the documentation and examples for more complex tests !