How would I go about executing multiple script files in a single run?

Thank you for posting ! :pray:t3:

Let me see if I got this right. You’d like to be able to split your tests into multiple files and execute them as a test suite, right?

You’re on the right track with both of your alternatives, however; only the default function and options of the entry file will be read, as you’ve noticed. To go around this, you’d need to manually execute the default function of the other test files from your entry files default function.

I’ve provided a working example below, showcasing what you’re trying to accomplish.

start.js

import http from "k6/http";
import { sleep, check } from "k6";
import runTestOne from "./test1.js";
import runTestTwo from "./test2.js";

export default function() {
    runTestOne();
    runTestTwo();
};

It imports the exported default function from each test file and runs them as part of the default function of start.js. Your actual test files would then look a little something like this:

test1.js

import http from "k6/http";
import { sleep, check } from "k6";

export default function() {
  let res = http.get("http://test.loadimpact.com");
  sleep(1);

  check(res, {
      "status is 200": (r) => r.status === 200,
      "response body": (r) => r.body.indexOf("Feel free to browse")
  });
}

test2.js

import http from "k6/http";
import { sleep, check } from "k6";

export default function() {
  let res = http.get("http://something-else.example.com");
  sleep(1);

  check(res, {
      "status is 200": (r) => r.status === 200,
      "response body": (r) => r.body.indexOf("Im something completely different")
  });
}

Feel free to reply to this thread if you need any additional assistance. :+1:t2:

1 Like