简体   繁体   中英

How can I overwrite data in csv file using jmeter

I am using Jmeter for API automation, I am writing result pass or fail in CSV file.

I am unable to overwrite old result data, Every time I run the test cases it's appending with old result data.

I use Beanshell Post processor for writing in CSV file.

import java.io.file;
import org.apache.jmeter.services.FileServer;

ActualResponseCode = prev.getResponseCode();


if (vars.get("ExpectedResponse").equals(vars.get("ActualResponse"))) 

{
if(vars.get("ExpectedResponseCode").equals(prev.getResponseCode()))
    {
        prev.setSuccessful(true);
        Result = "Pass";
         ErrorMessage = "No Error"; 
    }           
    else
    {
        Result = "Fail";
        ErrorMessage = "ResponseCode not matching";

    }

}   
else 
{
    prev.setSuccessful(false);
    Result = "Fail";
    ErrorMessage = "ResponseData is not matching";
}


f = new FileOutputStream("C://Users//a622821//Desktop//apache-jmeter-3.2//API_AUTOMATION//TestResult_Post.csv", true);

p = new PrintStream(f);
p.println(vars.get("TestCase") + "," + vars.get("API_Endpoint") + "," + vars.get("ExpectedResponseCode") + "," + ActualResponseCode + "," + Result + "," + ErrorMessage);

p.close();
f.close();

Basically when you write to file you can overrid file by using boolean false as second parameter to FileWriter constructor

File file = ....

new FileWriter(file, false);

If you have several calls for beanshell I suggest create Beanshell sampler which will be called in start of the test and will override the file (create empty file):

import java.io.file;
f = new FileOutputStream("C://Users//a622821//Desktop//apache-jmeter-3.2//API_AUTOMATION//TestResult_Post.csv", false);

p = new PrintStream(f);    

p.close();
f.close();

If you must use same beanshell you need a variable flag, so add variable firstTime with value true

In Beanshell use it to set the flag by firstTime variable:

import java.io.file;
import org.apache.jmeter.services.FileServer;

ActualResponseCode = prev.getResponseCode();


if (vars.get("ExpectedResponse").equals(vars.get("ActualResponse"))) 

{
if(vars.get("ExpectedResponseCode").equals(prev.getResponseCode()))
    {
        prev.setSuccessful(true);
        Result = "Pass";
         ErrorMessage = "No Error"; 
    }           
    else
    {
        Result = "Fail";
        ErrorMessage = "ResponseCode not matching";

    }

}   
else 
{
    prev.setSuccessful(false);
    Result = "Fail";
    ErrorMessage = "ResponseData is not matching";
}

firstTime = vars.get("firstTime");

flag = true;
if ("true".equals(firstTime)) {
  flag = false;
  vars.put("firstTime", "false");
}
f = new FileOutputStream("C://Users//a622821//Desktop//apache-jmeter-3.2//API_AUTOMATION//TestResult_Post.csv", flag);


p = new PrintStream(f);
p.println(vars.get("TestCase") + "," + vars.get("API_Endpoint") + "," + vars.get("ExpectedResponseCode") + "," + ActualResponseCode + "," + Result + "," + ErrorMessage);

p.close();
f.close();

Test Plan variable: 变量

Don't use scripting to write anything to files, JMeter is able to store literally anything into its .jtl results file so I would recommend rather configuring it to store what you need there rather than trying to create an extra results file in such a weird manner as:


So I would suggest switching to either Response Assertion or JSR223 Assertion instead of the PostProcessor. If required you can tell JMeter to store assertion failure message by adding the next line to user.properties file (located in the "bin" folder of JMeter installation, however I think it defaults to true in any case)

jmeter.save.saveservice.assertion_results_failure_message=true

Check out Scripting JMeter Assertions in Groovy - A Tutorial for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM