Skip to content

Commit 55dbad7

Browse files
Add a new guide on creating a script using the openapi-to-k6 converter (#2053)
* Add guide on using the openapi-to-k6 converter * Update docs/sources/k6/next/using-k6/test-authoring/create-test-script-using-openapi.md Co-authored-by: Théo Crevon <[email protected]> * Apply to previous versions --------- Co-authored-by: Théo Crevon <[email protected]>
1 parent e54dce7 commit 55dbad7

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Create a test script from an OpenAPI definition file
3+
description: Learn how to create a test using an OpenAPI definition file and the openapi-to-k6 tool.
4+
weight: 150
5+
---
6+
7+
# Create a test script from an OpenAPI definition file
8+
9+
Creating a k6 test script from scratch requires some JavaScript/TypeScript knowledge, as well as understanding k6 concepts such as the test lifecycle and k6 JavaScript APIs.
10+
11+
The OpenAPI specification has become popular over the years and is commonly used across development teams. It lets developers create a schema that includes details about an API, including authentication methods, endpoints, responses, and more. Once teams have an OpenAPI definition file, they can also use third-party tools to automatically create documentation, integrate with testing tools, or create mock servers.
12+
13+
The [openapi-to-k6](https://github.com/grafana/openapi-to-k6) project lets you use an existing OpenAPI schema to generate a TypeScript client that can be used to quickly test API endpoints with k6. The generated client exports a class with methods for each endpoint in the OpenAPI schema. You can import the client into your script, create an instance of the class, and use the methods to call any endpoints.
14+
15+
In this guide, you'll learn how to use the openapi-to-k6 project to generate a client and a sample script, and run the sample script to test your API endpoints.
16+
17+
## Before you begin
18+
19+
To use openapi-to-k6, you'll need to:
20+
21+
- [Install k6](https://grafana.com/docs/k6/<K6_VERSION>/set-up/install-k6/)
22+
- [Install npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
23+
24+
You'll also need an OpenAPI definition file, or the URL to an OpenAPI definition file. openapi-to-k6 supports JSON and YAML files.
25+
26+
## Install openapi-to-k6
27+
28+
To install openapi-to-k6, open your terminal and run:
29+
30+
```bash
31+
npm install -g @grafana/openapi-to-k6
32+
```
33+
34+
That installs openapi-to-k6 globally in your system. You can then run the following command to check that it was installed correctly:
35+
36+
```bash
37+
openapi-to-k6 --version
38+
```
39+
40+
## Generate a client from an OpenAPI schema
41+
42+
To generate a client using openapi-to-k6, you'll need an OpenAPI definition file in your system or a URL to an OpenAPI definition file. Open your terminal application and run:
43+
44+
```bash
45+
openapi-to-k6 --include-sample-script <OPENAPI_SCHEMA_PATH | OPENAPI_SCHEMA_URL> <OUTPUT_PATH>
46+
```
47+
48+
Make sure to replace:
49+
50+
- `<OPENAPI_SCHEMA_PATH | OPENAPI_SCHEMA_URL>` with the path or URL for your OpenAPI definition file.
51+
- `<OUTPUT_PATH>` with the path where you want the generated client to be saved.
52+
53+
In the output path, you'll see two generated TypeScript files:
54+
55+
- <OPENAPI_TITLE>.ts - the generated client uses the title property from the OpenAPI schema as its name. You can import this file into a k6 test script, and then generate scripts using specific endpoints you want to test.
56+
- script.sample.ts - the sample script generated by openapi-to-k6. This script is an example of how to import and use the generated client in a k6 test script.
57+
58+
## Run the sample test script
59+
60+
You can run the generated test script with k6. Open your terminal, and run:
61+
62+
```bash
63+
k6 run script.sample.ts
64+
```
65+
66+
Make sure to replace `script.sample.ts` with the name of your generated test script. Depending on the API you're testing and the OpenAPI schema that you're using, you may need to edit the TypeScript file to update any authentication parameters or headers to run the test script successfully.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Create a test script from an OpenAPI definition file
3+
description: Learn how to create a test using an OpenAPI definition file and the openapi-to-k6 tool.
4+
weight: 150
5+
---
6+
7+
# Create a test script from an OpenAPI definition file
8+
9+
Creating a k6 test script from scratch requires some JavaScript/TypeScript knowledge, as well as understanding k6 concepts such as the test lifecycle and k6 JavaScript APIs.
10+
11+
The OpenAPI specification has become popular over the years and is commonly used across development teams. It lets developers create a schema that includes details about an API, including authentication methods, endpoints, responses, and more. Once teams have an OpenAPI definition file, they can also use third-party tools to automatically create documentation, integrate with testing tools, or create mock servers.
12+
13+
The [openapi-to-k6](https://github.com/grafana/openapi-to-k6) project lets you use an existing OpenAPI schema to generate a TypeScript client that can be used to quickly test API endpoints with k6. The generated client exports a class with methods for each endpoint in the OpenAPI schema. You can import the client into your script, create an instance of the class, and use the methods to call any endpoints.
14+
15+
In this guide, you'll learn how to use the openapi-to-k6 project to generate a client and a sample script, and run the sample script to test your API endpoints.
16+
17+
## Before you begin
18+
19+
To use openapi-to-k6, you'll need to:
20+
21+
- [Install k6](https://grafana.com/docs/k6/<K6_VERSION>/set-up/install-k6/)
22+
- [Install npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
23+
24+
You'll also need an OpenAPI definition file, or the URL to an OpenAPI definition file. openapi-to-k6 supports JSON and YAML files.
25+
26+
## Install openapi-to-k6
27+
28+
To install openapi-to-k6, open your terminal and run:
29+
30+
```bash
31+
npm install -g @grafana/openapi-to-k6
32+
```
33+
34+
That installs openapi-to-k6 globally in your system. You can then run the following command to check that it was installed correctly:
35+
36+
```bash
37+
openapi-to-k6 --version
38+
```
39+
40+
## Generate a client from an OpenAPI schema
41+
42+
To generate a client using openapi-to-k6, you'll need an OpenAPI definition file in your system or a URL to an OpenAPI definition file. Open your terminal application and run:
43+
44+
```bash
45+
openapi-to-k6 --include-sample-script <OPENAPI_SCHEMA_PATH | OPENAPI_SCHEMA_URL> <OUTPUT_PATH>
46+
```
47+
48+
Make sure to replace:
49+
50+
- `<OPENAPI_SCHEMA_PATH | OPENAPI_SCHEMA_URL>` with the path or URL for your OpenAPI definition file.
51+
- `<OUTPUT_PATH>` with the path where you want the generated client to be saved.
52+
53+
In the output path, you'll see two generated TypeScript files:
54+
55+
- <OPENAPI_TITLE>.ts - the generated client uses the title property from the OpenAPI schema as its name. You can import this file into a k6 test script, and then generate scripts using specific endpoints you want to test.
56+
- script.sample.ts - the sample script generated by openapi-to-k6. This script is an example of how to import and use the generated client in a k6 test script.
57+
58+
## Run the sample test script
59+
60+
You can run the generated test script with k6. Open your terminal, and run:
61+
62+
```bash
63+
k6 run script.sample.ts
64+
```
65+
66+
Make sure to replace `script.sample.ts` with the name of your generated test script. Depending on the API you're testing and the OpenAPI schema that you're using, you may need to edit the TypeScript file to update any authentication parameters or headers to run the test script successfully.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Create a test script from an OpenAPI definition file
3+
description: Learn how to create a test using an OpenAPI definition file and the openapi-to-k6 tool.
4+
weight: 150
5+
---
6+
7+
# Create a test script from an OpenAPI definition file
8+
9+
Creating a k6 test script from scratch requires some JavaScript/TypeScript knowledge, as well as understanding k6 concepts such as the test lifecycle and k6 JavaScript APIs.
10+
11+
The OpenAPI specification has become popular over the years and is commonly used across development teams. It lets developers create a schema that includes details about an API, including authentication methods, endpoints, responses, and more. Once teams have an OpenAPI definition file, they can also use third-party tools to automatically create documentation, integrate with testing tools, or create mock servers.
12+
13+
The [openapi-to-k6](https://github.com/grafana/openapi-to-k6) project lets you use an existing OpenAPI schema to generate a TypeScript client that can be used to quickly test API endpoints with k6. The generated client exports a class with methods for each endpoint in the OpenAPI schema. You can import the client into your script, create an instance of the class, and use the methods to call any endpoints.
14+
15+
In this guide, you'll learn how to use the openapi-to-k6 project to generate a client and a sample script, and run the sample script to test your API endpoints.
16+
17+
## Before you begin
18+
19+
To use openapi-to-k6, you'll need to:
20+
21+
- [Install k6](https://grafana.com/docs/k6/<K6_VERSION>/set-up/install-k6/)
22+
- [Install npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
23+
24+
You'll also need an OpenAPI definition file, or the URL to an OpenAPI definition file. openapi-to-k6 supports JSON and YAML files.
25+
26+
## Install openapi-to-k6
27+
28+
To install openapi-to-k6, open your terminal and run:
29+
30+
```bash
31+
npm install -g @grafana/openapi-to-k6
32+
```
33+
34+
That installs openapi-to-k6 globally in your system. You can then run the following command to check that it was installed correctly:
35+
36+
```bash
37+
openapi-to-k6 --version
38+
```
39+
40+
## Generate a client from an OpenAPI schema
41+
42+
To generate a client using openapi-to-k6, you'll need an OpenAPI definition file in your system or a URL to an OpenAPI definition file. Open your terminal application and run:
43+
44+
```bash
45+
openapi-to-k6 --include-sample-script <OPENAPI_SCHEMA_PATH | OPENAPI_SCHEMA_URL> <OUTPUT_PATH>
46+
```
47+
48+
Make sure to replace:
49+
50+
- `<OPENAPI_SCHEMA_PATH | OPENAPI_SCHEMA_URL>` with the path or URL for your OpenAPI definition file.
51+
- `<OUTPUT_PATH>` with the path where you want the generated client to be saved.
52+
53+
In the output path, you'll see two generated TypeScript files:
54+
55+
- <OPENAPI_TITLE>.ts - the generated client uses the title property from the OpenAPI schema as its name. You can import this file into a k6 test script, and then generate scripts using specific endpoints you want to test.
56+
- script.sample.ts - the sample script generated by openapi-to-k6. This script is an example of how to import and use the generated client in a k6 test script.
57+
58+
## Run the sample test script
59+
60+
You can run the generated test script with k6. Open your terminal, and run:
61+
62+
```bash
63+
k6 run script.sample.ts
64+
```
65+
66+
Make sure to replace `script.sample.ts` with the name of your generated test script. Depending on the API you're testing and the OpenAPI schema that you're using, you may need to edit the TypeScript file to update any authentication parameters or headers to run the test script successfully.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Create a test script from an OpenAPI definition file
3+
description: Learn how to create a test using an OpenAPI definition file and the openapi-to-k6 tool.
4+
weight: 150
5+
---
6+
7+
# Create a test script from an OpenAPI definition file
8+
9+
Creating a k6 test script from scratch requires some JavaScript/TypeScript knowledge, as well as understanding k6 concepts such as the test lifecycle and k6 JavaScript APIs.
10+
11+
The OpenAPI specification has become popular over the years and is commonly used across development teams. It lets developers create a schema that includes details about an API, including authentication methods, endpoints, responses, and more. Once teams have an OpenAPI definition file, they can also use third-party tools to automatically create documentation, integrate with testing tools, or create mock servers.
12+
13+
The [openapi-to-k6](https://github.com/grafana/openapi-to-k6) project lets you use an existing OpenAPI schema to generate a TypeScript client that can be used to quickly test API endpoints with k6. The generated client exports a class with methods for each endpoint in the OpenAPI schema. You can import the client into your script, create an instance of the class, and use the methods to call any endpoints.
14+
15+
In this guide, you'll learn how to use the openapi-to-k6 project to generate a client and a sample script, and run the sample script to test your API endpoints.
16+
17+
## Before you begin
18+
19+
To use openapi-to-k6, you'll need to:
20+
21+
- [Install k6](https://grafana.com/docs/k6/<K6_VERSION>/set-up/install-k6/)
22+
- [Install npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
23+
24+
You'll also need an OpenAPI definition file, or the URL to an OpenAPI definition file. openapi-to-k6 supports JSON and YAML files.
25+
26+
## Install openapi-to-k6
27+
28+
To install openapi-to-k6, open your terminal and run:
29+
30+
```bash
31+
npm install -g @grafana/openapi-to-k6
32+
```
33+
34+
That installs openapi-to-k6 globally in your system. You can then run the following command to check that it was installed correctly:
35+
36+
```bash
37+
openapi-to-k6 --version
38+
```
39+
40+
## Generate a client from an OpenAPI schema
41+
42+
To generate a client using openapi-to-k6, you'll need an OpenAPI definition file in your system or a URL to an OpenAPI definition file. Open your terminal application and run:
43+
44+
```bash
45+
openapi-to-k6 --include-sample-script <OPENAPI_SCHEMA_PATH | OPENAPI_SCHEMA_URL> <OUTPUT_PATH>
46+
```
47+
48+
Make sure to replace:
49+
50+
- `<OPENAPI_SCHEMA_PATH | OPENAPI_SCHEMA_URL>` with the path or URL for your OpenAPI definition file.
51+
- `<OUTPUT_PATH>` with the path where you want the generated client to be saved.
52+
53+
In the output path, you'll see two generated TypeScript files:
54+
55+
- <OPENAPI_TITLE>.ts - the generated client uses the title property from the OpenAPI schema as its name. You can import this file into a k6 test script, and then generate scripts using specific endpoints you want to test.
56+
- script.sample.ts - the sample script generated by openapi-to-k6. This script is an example of how to import and use the generated client in a k6 test script.
57+
58+
## Run the sample test script
59+
60+
You can run the generated test script with k6. Open your terminal, and run:
61+
62+
```bash
63+
k6 run script.sample.ts
64+
```
65+
66+
Make sure to replace `script.sample.ts` with the name of your generated test script. Depending on the API you're testing and the OpenAPI schema that you're using, you may need to edit the TypeScript file to update any authentication parameters or headers to run the test script successfully.

0 commit comments

Comments
 (0)