[ Web Proxy ]
URL:
Viewing: https://java.testcontainers.org/supported_docker_environment/../../../modules/k6/ [Back]  [Original]

k6 Module - Testcontainers for Java
Skip to content
Testcontainers [Testcontainers] Menu
testcontainers-java
Content

k6 Module

Note

This module is INCUBATING. While it is ready for use and operational in the current version of Testcontainers, it is possible that it may receive breaking changes in the future. See our contributing guidelines for more information on our incubating modules policy.

Testcontainers module for k6.

k6 is an extensible reliability testing tool built for developer happiness.

Basic script execution

You can start a K6 container instance from any Java application by using:

Setup the container
K6Container container = new K6Container("grafana/k6:0.49.0")
    .withTestScript(MountableFile.forClasspathResource("scripts/test.js"))
    .withScriptVar("MY_SCRIPT_VAR", "are cool!")
    .withScriptVar("AN_UNUSED_VAR", "unused")
    .withCmdOptions("--quiet", "--no-usage-report")

The test above uses a simple k6 script, test.js, with command line options and an injected script variable.

Once the container is started, you can wait for the test results to be collected:

Wait for test results
WaitingConsumer consumer = new WaitingConsumer();
container.followOutput(consumer);

// Wait for test script results to be collected
consumer.waitUntil(
    frame -> {
        return frame.getUtf8String().contains("iteration_duration");
    },
    3,
    TimeUnit.SECONDS
);

Create a simple k6 test script to be executed as part of your tests:

Content of scripts/test.js
// The most basic of k6 scripts.
export default function(){
    console.log(`k6 tests ${__ENV.MY_SCRIPT_VAR}`)
}

Adding this module to your project dependencies

Add the following dependency to your pom.xml/build.gradle file:

GradleMaven
testImplementation "org.testcontainers:testcontainers-k6:2.0.5"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers-k6</artifactId>
    <version>2.0.5</version>
    <scope>test</scope>
</dependency>

Web Proxy Viewer  |  New URL  |  Original Page