Skip to main content

k6 new

Create and initialize a new k6 script using one of the predefined templates.

Synopsis

k6 new [flags] [file]

Description

The k6 new command creates a new test script from a template. By default, it creates a file named script.js unless a different filename is specified. This command helps you quickly get started with k6 by providing pre-configured templates for common testing scenarios.

Arguments

file
string
default:"script.js"
The name of the script file to create

Examples

# Create a new k6 script with the default template
k6 new

# Specify a file name when creating a script
k6 new test.js

# Overwrite an existing file
k6 new -f test.js

# Create a script using a specific template
k6 new --template protocol

# Create a cloud-ready script with a specific project ID
k6 new --project-id 12315

Flags

-f, --force
boolean
Overwrite existing files without prompting
--template
string
default:"minimal"
Template type to use. Available options:
  • minimal - Basic k6 test script
  • protocol - Protocol-level testing template
  • browser - Browser automation template
  • Path to a custom template file (relative or absolute)
--project-id
string
Specify the Grafana Cloud project ID for the test. This pre-configures the script for cloud execution.

Template Types

minimal

The minimal template creates a simple load test with basic HTTP requests. This is ideal for getting started quickly.

protocol

The protocol template includes examples of various HTTP methods, request configuration, and response validation.

browser

The browser template sets up a browser-based test using k6’s browser testing capabilities.

Custom Templates

You can also provide a path to your own custom template file:
k6 new --template ./templates/my-template.js my-test.js

Output

When successful, the command outputs:
New script created: <filename> (<template> template).

Error Handling

The command will fail if:
  • The target file already exists and --force is not specified
  • The specified template doesn’t exist or is invalid
  • There are file system permission issues

See Also

  • k6 run - Run your newly created test
  • k6 cloud - Run tests in Grafana Cloud

Build docs developers (and LLMs) love