Groovy(SoapUI)中file.append写入CSV为何重复输出'hello world'?
Why does my Groovy script in SoapUI write duplicate content to CSV?
Let me break down why you're seeing hello world hello world instead of a single entry in your CSV file:
Common Causes
- Your script is running twice
This is the most likely culprit. SoapUI can trigger script execution more than once in a few scenarios:- You’ve set your TestCase/TestSuite to run multiple times (check the execution settings in the top toolbar when you launch your tests).
- The Groovy Script test step is duplicated in your TestCase, or you’ve placed the same file-writing code in both a Setup Script (for the TestCase/Suite) and a regular Test Step.
- In rare cases, custom listeners or SoapUI plugins might be triggering extra executions, though this is far less common.
- Accidental duplicate triggers
If you’re running tests manually, you might have clicked the run button twice without noticing, or the test runner is re-running failed steps automatically (double-check settings like "Run failed tests only").
How to Diagnose & Fix
Add a log statement to confirm execution count
Modify your script to log when it runs—this will show you exactly how many times it executes:log.info "Starting CSV write operation" def f = new File(project.rootPath + "/outputTest1.csv") f.append("hello world" + '\n')Check the Log tab in SoapUI; if you see two "Starting CSV write operation" entries, that confirms the script is running twice.
Audit your test structure
- Walk through your TestCase steps: make sure you only have one Groovy Script step with this code.
- Check the Setup Script and TearDown Script tabs for your TestCase/TestSuite—ensure the same file-writing code isn’t present there too.
- Verify your run configuration: when you click "Run", check if the "Repeat Test" option is set to a number greater than 1.
Prevent duplicate writes if needed
If you intentionally run the test multiple times but don’t want duplicate lines, you have two quick options:- Clear the file before writing (only do this if you don’t need to keep existing content):
def f = new File(project.rootPath + "/outputTest1.csv") f.text = "hello world" + '\n' // Overwrites existing content instead of appending - Or add a check to only write if the line doesn’t already exist (great for preserving existing data):
def f = new File(project.rootPath + "/outputTest1.csv") def content = f.text if (!content.contains("hello world")) { f.append("hello world" + '\n') }
- Clear the file before writing (only do this if you don’t need to keep existing content):
内容的提问来源于stack exchange,提问作者Shaun Cromie
相关产品推荐
相关产品推荐

