/k6:generating-tests-from-openapifrom k6
Generates k6 load test scripts from OpenAPI 3.0/3.1 specs (YAML/JSON), mapping endpoints, parameters, security schemes, and tags to k6 scenarios with version-aware syntax.
Ships in k6. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Generate k6 load test scripts by parsing OpenAPI specification files (YAML format). Extracts endpoints, parameters, request bodies, and security schemes to create comprehensive test suites.
Written for k6 v2.3.0 (current stable). The v1.8.x maintenance line is supported where noted; differences are marked v1.x: inline. Before generating, determine the version of the environment that will run the test:
k6 version where the test will execute (prints k6 v2.3.0 (...)). For Docker, CI, or k6-operator, read the pinned image tag, the setup-k6-action k6-version input, or spec.runner.image.// Target: k6 v2.3.0.Version notes for this skill:
tag to its own scenario so users can run a subset with k6 run --scenario Users,Orders script.js (v2.3.0+). v1.x: add an __ENV.SCENARIO switch.k6 inspect script.js, then k6 run --once script.js (v2.3.0+). v1.x: k6 run --vus 1 --iterations 1 script.js.securitySchemes of type http/basic become an Authorization: Basic ... header or URL credentials; never auth: 'basic' (no effect).QUERY method map to http.query() (documented for k6 v2.3.x); on older versions fall back to http.request('QUERY', ...) only if the server accepts it, otherwise skip them with a note.For the full feature/version matrix see /k6:operating-k6-in-ci-cd (reference/version-compatibility.md).
Read the OpenAPI YAML file and extract:
Before generating test scripts, analyze security requirements.
Extract security definitions from components.securitySchemes:
Map per-endpoint security:
security arraysecurity overridessecurity: [])Generate k6 auth setup:
setup() function matching the required auth flowSee reference/openapi-auth-mapping.md for mapping security schemes to k6 code.
For each path and operation, extract:
paths:
/users:
get: # HTTP method
operationId: listUsers # Operation identifier
tags: [Users] # Grouping
parameters: # Query/header/path params
- name: page
in: query
schema:
type: integer
security: # Per-operation security
- bearerAuth: []
responses:
'200':
description: Success
Map to k6:
get/post/put/patch/del/head/options, query for QUERY on k6 v2.3.x)http.post() body (JSON.stringify + Content-Type: application/json; a bare object is form-encoded)--scenario, v2.3.0+) with groups insideoperationId → request tags: { name: operationId } so dynamic path parameters do not fragment metricsConvert schema definitions to example values:
| Schema Type | Example Value |
|---|---|
string | "test-string" |
string (email) | "[email protected]" |
string (date-time) | "2024-01-01T00:00:00Z" |
string (uuid) | "550e8400-e29b-41d4-a716-446655440000" |
integer | 1 |
number | 1.0 |
boolean | true |
array | [<item_example>] |
object | {<property_examples>} |
enum | First enum value |
Use example or default values from the spec when available.
See reference/openapi-parsing.md for detailed parsing logic.
Organize the generated script by API tags, one scenario per tag:
// Target: k6 v2.3.0
import http from 'k6/http';
import { check, group, sleep } from 'k6';
const BASE_URL = __ENV.BASE_URL || 'https://api.example.com';
export const options = {
scenarios: {
// Run one tag only: k6 run --scenario users script.js
users: {
executor: 'ramping-vus',
exec: 'users',
startVUs: 0,
stages: [
{ duration: '2m', target: 10 },
{ duration: '5m', target: 10 },
{ duration: '1m', target: 0 },
],
},
},
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
export function setup() {
// Auth flow based on security scheme
}
export function users(data) {
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${data.token}`,
};
group('Users', () => {
// GET /users
const listRes = http.get(`${BASE_URL}/users?page=1&limit=10`, {
headers, tags: { name: 'ListUsers' },
});
check(listRes, { 'list users 200': (r) => r.status === 200 });
// POST /users
const createRes = http.post(`${BASE_URL}/users`,
JSON.stringify({ name: 'Test User', email: '[email protected]' }),
{ headers, tags: { name: 'CreateUser' } }
);
check(createRes, { 'create user 201': (r) => r.status === 201 });
// GET /users/{id}
const userId = createRes.json('id') || 1;
const getRes = http.get(`${BASE_URL}/users/${userId}`, {
headers, tags: { name: 'GetUser' },
});
check(getRes, { 'get user 200': (r) => r.status === 200 });
});
sleep(1);
}
Based on endpoint characteristics:
Because each tag is a scenario, the same script serves every stage: k6 run --scenario users script.js while iterating on one resource, k6 run --scenario users,orders --once script.js as a smoke check, plain k6 run script.js for the full load (v2.3.0+).
k6 inspect script.js — parse check and resolved optionsk6 run --once script.js (v2.3.0+) against a dev server, or k6 run --scenario <tag> --once script.js for a single tag. v1.x: k6 run --vus 1 --iterations 1 script.jsresponses declared in the spec and report mismatchesopenapi: '3.1.0'
info:
title: My API
version: '1.0.0'
servers:
- url: https://api.example.com/v1
security:
- bearerAuth: [] # Global security
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
apiKey:
type: apiKey
in: header
name: X-API-Key
schemas:
User:
type: object
required: [name, email]
properties:
id:
type: integer
name:
type: string
email:
type: string
format: email
paths:
/users:
get:
tags: [Users]
parameters:
- name: page
in: query
schema: { type: integer, default: 1 }
responses:
'200':
description: User list
post:
tags: [Users]
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/User'
responses:
'201':
description: Created
/users/{id}:
get:
tags: [Users]
parameters:
- name: id
in: path
required: true
schema: { type: integer }
responses:
'200':
description: User details
/k6:generating-api-load-tests/k6:designing-test-scenarios/k6:generating-tests-from-codeWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
8 skills in this plugin
/analyzing-test-resultsUse when interpreting k6 test output, analyzing performance metrics, identifying bottlenecks, tuning thresholds, or optimizing k6 test scripts. Use when the user has k6 test results and needs to understand response times, error rates, percentiles (p95, p99), or performance degradation patterns./designing-test-scenariosUse when designing k6 load profiles, choosing executors, configuring VU patterns, setting thresholds, or planning test scenarios. Use when the user asks about stress test, spike test, soak test, breakpoint test, load model, executor selection, or pass/fail criteria./generating-api-load-testsnpx claudepluginhub kimdoubleb/grafana-k6-skills --plugin k6Other plugins' skills for the same job