Data-Driven k6 Tests: Loading External CSV Files with SharedArray
Load unique test data from an external CSV into k6 using open() and SharedArray so each virtual user sends different credentials or payloads.
27 Apr 2026, 19:58 UTC

Problem and Takeaway
Running a k6 load test where every virtual user sends the same username, product ID, or session token produces uniform traffic that can hide failures triggered by specific data values. The useful takeaway is to load an external CSV file once with k6's open() function and a SharedArray, then assign a different row to each virtual user iteration so the test exercises real data variation.
Prerequisites
- k6 installed and available on the system
PATH. - A CSV file containing test data, for example
users.csvwith columns such asusernameandpassword. - Read permission on the CSV file and network access from the k6 host to the target endpoint.
- A target API endpoint that accepts the request payload you are testing.
SharedArray is a k6 type that holds data in memory shared across all virtual users, avoiding the cost of loading the same file separately for every user.
Procedure: Load CSV Data and Drive Requests
Create the data file. Save it as
users.csvin the project directory:username,password alice,pass-alpha bob,pass-beta carol,pass-gammaDo not commit files containing real credentials to version control.
Write the k6 script. Use
open()during initialization to read the file, wrap it inSharedArray, and select a row by iteration index.import http from 'k6/http'; import { check, sleep } from 'k6'; import { SharedArray } from 'k6/data'; const users = new SharedArray('users', function () { return open('./users.csv').split('\n').slice(1).map(line => { const [username, password] = line.split(','); return { username, password }; }).filter(user => user.username !== undefined); }); export const options = { vus: 3, duration: '10s', }; export default function () { const user = users[__ITER % users.length]; const payload = JSON.stringify({ username: user.username, password: user.password, }); const res = http.post('http://localhost:8080/login', payload, { headers: { 'Content-Type': 'application/json' }, }); check(res, { 'status is 200': (r) => r.status === 200, }); sleep(1); }Replace
http://localhost:8080/loginwith your target endpoint.__ITERis the zero-based iteration counter; the modulo operator keeps the index inside the array bounds if virtual users request more rows than the file contains.Run the test from the directory containing the script and CSV:
k6 run script.jsRun this in a terminal on the machine executing the load generator. No special privileges are required beyond reading the CSV and making outbound HTTP connections.
Expected Checks
- The CLI summary shows the expected number of iterations.
- No error such as "not enough data" appears if you instead index directly without modulo and exceed row count.
- Response checks pass for the rows actually sent; inspect individual request results if a specific username triggers failures.
Diagnostic Decision
If failures cluster on one row, the issue is likely tied to that data value rather than overall load. If failures appear across random rows, the problem is more likely concurrency or infrastructure-related. Add a console.log(user.username) temporarily to confirm which row each iteration used, then remove it before running at scale because logging can slow execution.
Limitations
open()runs at initialization, so changes to the CSV after the test starts are not picked up.SharedArrayis read-only; virtual users cannot update the shared data during the test.- Very large CSV files increase startup memory because the full array is loaded before VUs begin.
- CSV parsing with
split(',')does not handle quoted fields containing commas; use simple data without embedded commas or a proper parsing approach.
Practical Verification
Run a smoke test with vus: 1 and iterations: 1 instead of duration, then confirm the request body contains the first CSV row. This verifies the file path, column mapping, and payload construction before running a larger load.
Recovery Options
Running k6 does not modify k6 internal state, so there is no k6-specific rollback. If the test writes records to the target system, clean up those test records externally. To recover from a script error, correct the CSV path or column mapping in the JavaScript file and rerun k6 run script.js.