Search code examples
jenkinsjmeterbeanshell

JMeter(Bean Shell Sampler) + Git + Jenkins - Bean Sampler script not Reading the csv file


I have API tests created in Jmeter and committed to the GIT branch. All my JMeter tests executed from the Jenkins Pipeline job.

ISSUE: In the Jmeter test, I am using a bean shell sampler that reads data from a CSV file. Refer to the below code.

LineNumberReader lineReader = new LineNumberReader(new FileReader(new File("${csv}")));
String line = null;
int count = 1;
while ((line = lineReader.readLine()) != null) {
 String[] Test_details = line.split(",");
 props.put("Test_id_" + count, Test_details[0]);
 props.put("scenario_id_" + count, Test_details[1]);
 count++;
}
lineReader.close();

In this above script, CSV path referred from User-defined variables in Jmeter (csv = 'FilePath')

When I execute tests from Jenkins, data from the CSV file are not retrieved and tests are failing.

Note: When I execute from the local machine, data from CSV is retrieved and tests get passed.

While executing from Jenkins below CSV path is given in user-defined variables. csv = apitests\src\test\jmeter\data\Testdata.csv

and below is the folder structure Folder Struture


Solution

  • If you're running test via JMeter Maven plugin the test base directory (a.k.a Java user.dir) looks like:

    target/somerandomguid/jmeter/bin
    

    so if you need to get the relative path to your file you need to get 4 levels up and then go to src/test/jmeter/data, something like:

    new File("../../../../src/test/jmeter/data/Testdata.csv")
    

    Also be aware that starting from JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so it might be a good chance for the change. More information: Apache Groovy - Why and How You Should Use It