admin管理员组

文章数量:1278854

Is there a best practice way to read csv files for test data?

Say I have a CSV file that looks like this:

username,password
joe,secret1
jane,secret2
bill,secret3

...and I want to use those usernames and passwords inside a k6 script.

Is there a best practice way to read csv files for test data?

Say I have a CSV file that looks like this:

username,password
joe,secret1
jane,secret2
bill,secret3

...and I want to use those usernames and passwords inside a k6 script.

Share Improve this question asked Nov 7, 2017 at 11:53 RagnarRagnar 1,1822 gold badges9 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Yes! You'd use the open() function in the init context (outside export default function() {...}). Here's an example if you'd have JSON data in a file:

import { sleep } from "k6";

const data = JSON.parse(open("./data.json"));

export default function() {
  let user = data[__VU - 1];
  console.log(`${user.username}, ${user.password}`);
  sleep(3);
}

...if you have a data file data.json looking something like this:

[
  {
   "username" : "user1", 
   "password" : "test" 
  },
  {
   "username" : "user2", 
   "password" : "test" 
  }
]

To do the same thing with CSV, I'd look for a JS CSV parsing library and import it as a module

本文标签: javascriptReading from a file in k6Stack Overflow