-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create tests in Insomnia #108
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: Write tests for content types in Insomnia | ||
related_resources: | ||
- text: Write tests for headers in the response in Insomnia | ||
url: /how-to/write-headers-in-response-test/ | ||
- text: Write tests for HTTP status codes in Insomnia | ||
url: /how-to/write-http-status-tests/ | ||
- text: Write tests for data types in Insomnia | ||
url: /how-to/write-data-type-tests/ | ||
- text: Automate tests in Insomnia | ||
url: /how-to/automate-tests/ | ||
- text: Chain requests in Insomnia | ||
url: /how-to/chain-requests/ | ||
|
||
products: | ||
- insomnia | ||
|
||
tags: | ||
- test-apis | ||
|
||
tldr: | ||
q: How do I write content type tests in Insomnia? | ||
a: After you add a collection, you can create a new test suite for the collection and then create individual tests in the suite. | ||
|
||
prereqs: | ||
inline: | ||
- title: Create and configure a collection | ||
include_content: prereqs/create-collection | ||
icon_url: /assets/icons/menu.svg | ||
cleanup: | ||
inline: | ||
- title: Clean up Insomnia | ||
include_content: cleanup/products/insomnia | ||
icon_url: /assets/icons/insomnia/insomnia.svg | ||
--- | ||
|
||
## 1. Create a test suite | ||
|
||
Before you create a test, you need to create a test suite for our collection. | ||
|
||
To do this, click the **Tests** tab and click **New test suite** in the sidebar. | ||
|
||
## 2. Create a top-level content type in body test | ||
|
||
Now you can test if a content type is returned in the top-level request body. | ||
|
||
1. Click **New test** and enter a name for the test, such as "Content type in body (top-level)". | ||
1. From the **Select a request** drop down, select the **GET KongAir planned flights** request. | ||
1. The following Javascript checks if the top-level array is present in the body of the response. Enter the following in the Javascript for your test: | ||
```javascript | ||
const response1 = await insomnia.send(); | ||
const body = JSON.parse(response1.data); | ||
const item = body[1]; | ||
expect(item).to.be.an('object'); | ||
``` | ||
1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes. | ||
|
||
## 2. Create a nested content type in body test | ||
|
||
Now you can test if a content type is returned in the nested request body. | ||
|
||
1. Click **New test** and enter a name for the test, such as "Content type in body (nested)". | ||
1. From the **Select a request** drop down, select the **GET Fetch more details about a flight** request. | ||
1. The following Javascript checks if the nested `meal_options` array is present in the body of the response. Enter the following in the Javascript for your test: | ||
```javascript | ||
const response1 = await insomnia.send(); | ||
const body = JSON.parse(response1.data); | ||
expect(body).to.have.property('meal_options'); | ||
expect(body.meal_options).to.be.an('array'); | ||
expect(body).to.be.an('object'); | ||
``` | ||
1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: Write tests for data types in Insomnia | ||
related_resources: | ||
- text: Write tests for headers in the response in Insomnia | ||
url: /how-to/write-headers-in-response-test/ | ||
- text: Write tests for HTTP status codes in Insomnia | ||
url: /how-to/write-http-status-tests/ | ||
- text: Write tests for content types in Insomnia | ||
url: /how-to/write-content-type-tests/ | ||
- text: Automate tests in Insomnia | ||
url: /how-to/automate-tests/ | ||
- text: Chain requests in Insomnia | ||
url: /how-to/chain-requests/ | ||
|
||
products: | ||
- insomnia | ||
|
||
tags: | ||
- test-apis | ||
|
||
tldr: | ||
q: How do I write data type tests in Insomnia? | ||
a: After you add a collection, you can create a new test suite for the collection and then create individual tests in the suite. | ||
|
||
prereqs: | ||
inline: | ||
- title: Create and configure a collection | ||
include_content: prereqs/create-collection | ||
icon_url: /assets/icons/menu.svg | ||
cleanup: | ||
inline: | ||
- title: Clean up Insomnia | ||
include_content: cleanup/products/insomnia | ||
icon_url: /assets/icons/insomnia/insomnia.svg | ||
--- | ||
|
||
## 1. Create a test suite | ||
|
||
Before you create a test, you need to create a test suite for our collection. | ||
|
||
To do this, click the **Tests** tab and click **New test suite** in the sidebar. | ||
|
||
## 2. Create a top-level data type in body test | ||
|
||
Now you can test if a data type is returned in the top-level request body. | ||
|
||
1. Click **New test** and enter a name for the test, such as "Data type in body (top-level)". | ||
1. From the **Select a request** drop down, select the **GET KongAir planned flights** request. | ||
1. The following Javascript checks if an array is present in the top-level body of the response. Enter the following in the Javascript for your test: | ||
```javascript | ||
const response1 = await insomnia.send(); | ||
const body = JSON.parse(response1.data); | ||
expect(body).to.be.an('array'); | ||
``` | ||
1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes. | ||
|
||
## 3. Create a top-level data type in body test | ||
|
||
Now you can test if a data type is returned in the top-level request body. | ||
|
||
1. Click **New test** and enter a name for the test, such as "Data type in body (nested)". | ||
1. From the **Select a request** drop down, select the **GET Fetch more details about a flight** request. | ||
1. The following Javascript checks if the first string in the `meal_options` array is present in the body of the response. Enter the following in the Javascript for your test: | ||
```javascript | ||
const response1 = await insomnia.send(); | ||
const body = JSON.parse(response1.data); | ||
expect(body).to.be.an('object'); | ||
expect(body).to.have.property('meal_options'); | ||
expect(body.meal_options).to.be.an('array'); | ||
if (body.meal_options.length > 0) { | ||
expect(body.meal_options[0]).to.be.a('string'); | ||
} | ||
``` | ||
Comment on lines
+64
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for reviewers: This was created with ChatGPT. Although the test passes, I don't fully trust it or understand it. Is Also, my gut is telling me that some of these lines might be extraneous/unnecessary. |
||
1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: Write tests for headers in the response in Insomnia | ||
related_resources: | ||
- text: Write tests for HTTP status codes in Insomnia | ||
url: /how-to/write-http-status-tests/ | ||
- text: Write tests for content types in Insomnia | ||
url: /how-to/write-content-type-tests/ | ||
- text: Write tests for data types in Insomnia | ||
url: /how-to/write-data-type-tests/ | ||
- text: Automate tests in Insomnia | ||
url: /how-to/automate-tests/ | ||
- text: Chain requests in Insomnia | ||
url: /how-to/chain-requests/ | ||
|
||
products: | ||
- insomnia | ||
|
||
tags: | ||
- test-apis | ||
|
||
tldr: | ||
q: How do I write tests for response headers in Insomnia? | ||
a: After you add a collection, you can create a new test suite for the collection and then create individual tests in the suite. | ||
|
||
prereqs: | ||
inline: | ||
- title: Create and configure a collection | ||
include_content: prereqs/create-collection | ||
icon_url: /assets/icons/menu.svg | ||
cleanup: | ||
inline: | ||
- title: Clean up Insomnia | ||
include_content: cleanup/products/insomnia | ||
icon_url: /assets/icons/insomnia/insomnia.svg | ||
--- | ||
|
||
## 1. Create a test suite | ||
|
||
Before you create a test, you need to create a test suite for our collection. | ||
|
||
To do this, click the **Tests** tab and click **New test suite** in the sidebar. | ||
|
||
## 2. Create a headers in response test | ||
|
||
Now you can test if a header is returned in the response. | ||
|
||
1. Click **New test** and enter a name for the test, such as "Header in the body". | ||
1. From the **Select a request** drop down, select the **GET KongAir planned flights** request. | ||
1. The following Javascript checks if there are any headers in the response. Enter the following in the Javascript for your test: | ||
```javascript | ||
const response1 = await insomnia.send(); | ||
expect(Object.keys(response1.headers).length).to.be.greaterThan(0); | ||
const body = JSON.parse(response1.data); | ||
const item = body[1]; | ||
expect(item).to.be.an('object'); | ||
``` | ||
1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: Write tests for HTTP status codes in Insomnia | ||
related_resources: | ||
- text: Write tests for headers in the response in Insomnia | ||
url: /how-to/write-headers-in-response-test/ | ||
- text: Write tests for content types in Insomnia | ||
url: /how-to/write-content-type-tests/ | ||
- text: Write tests for data types in Insomnia | ||
url: /how-to/write-data-type-tests/ | ||
- text: Automate tests in Insomnia | ||
url: /how-to/automate-tests/ | ||
- text: Chain requests in Insomnia | ||
url: /how-to/chain-requests/ | ||
|
||
products: | ||
- insomnia | ||
|
||
tags: | ||
- test-apis | ||
|
||
tldr: | ||
q: How do I write HTTP status code tests in Insomnia? | ||
a: After you add a collection, you can create a new test suite for the collection and then use the default Javascript test. | ||
|
||
prereqs: | ||
inline: | ||
- title: Create and configure a collection | ||
include_content: prereqs/create-collection | ||
icon_url: /assets/icons/menu.svg | ||
cleanup: | ||
inline: | ||
- title: Clean up Insomnia | ||
include_content: cleanup/products/insomnia | ||
icon_url: /assets/icons/insomnia/insomnia.svg | ||
--- | ||
|
||
## 1. Create a test suite | ||
|
||
Before you create a test, you need to create a test suite for our collection. | ||
|
||
To do this, click the **Tests** tab and click **New test suite** in the sidebar. | ||
|
||
## 2. Create a HTTP status code test | ||
|
||
You can create a test that checks a request against a certain status code. | ||
|
||
1. From the Test Suite you just created, click **New test**. Insomnia creates a default `Return 200` request for you: | ||
```javascript | ||
const response1 = await insomnia.send(); | ||
expect(response1.status).to.equal(200); | ||
``` | ||
1. From the **Select a request** drop down, select the **GET KongAir planned flights** request. | ||
1. Click the **Play** icon next to your test. In the preview to the right, you should see that the test passes. |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
If you created a new Collection and want to delete it, navigate to the collection, click it's name in the sidebar on the right, and then select **Delete**. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
1. Import the KongAir Flights spec in Insomnia: | ||
<a href="https://insomnia.rest/run/?label=&uri=https%3A%2F%2Fraw.githubusercontent.com%2FKong%2FKongAir%2Frefs%2Fheads%2Fmain%2Fflight-data%2Fflights%2Fopenapi.yaml" target="_blank"><img src="https://insomnia.rest/images/run.svg" alt="Run in Insomnia"></a> | ||
1. Click the **Settings** icon for **SPEC** in the sidebar, and then click **Generate collection**. | ||
1. Click **Base environment** and then click the **Edit** icon. | ||
1. Add the following content to the base environment: | ||
```json | ||
{ | ||
"base_url": "https://api.kong-air.com", | ||
"flightNumber": "KA0284" | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for reviewers: This was created with ChatGPT. Although the test passes, I don't fully trust it or understand it. Is
meal_options
really nested? Is this really testing the right thing?Also, my gut is telling me that some of these lines might be extraneous/unnecessary.