0Pricing
Load Testing & Performance Benchmarking (JMeter & k6) · 课时

使用 CSV 数据进行参数化

使用 CSV 数据集配置将动态输入数据注入测试脚本

使用 CSV 数据进行参数化 是 CoddyKit 上的免费 Load Testing & Performance Benchmarking (JMeter & k6) 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Load Testing & Performance Benchmarking (JMeter & k6) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Load Testing & Performance Benchmarking (JMeter & k6) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Dynamic Data with CSV

When performance testing, we often need to simulate many users doing similar, but not identical, actions. For example, logging in with different credentials or searching for different items.

This is where parameterization comes in! Instead of hardcoding values, we use variables to inject dynamic data into our tests. This lesson focuses on using CSV files for this purpose in JMeter.

What is Parameterization?

Parameterization is the process of replacing hardcoded values in your test script with variables. These variables then get their values from an external source, like a data file.

  • It makes your tests more realistic by simulating diverse user inputs.
  • It helps prevent caching issues on the server side, as each request can be unique.
  • It allows you to test different scenarios without modifying the test script itself.

Meet CSV Data Set Config

In JMeter, the primary element for parameterization using external files is the CSV Data Set Config. This element reads data from a plain text file, typically with comma-separated values (CSV).

It works by reading a new line of data for each iteration of your test or for each virtual user, making the requests unique.

Preparing Your CSV File

Your CSV file is just a simple text file. Each line represents a set of data for one iteration or one user. Values within a line are separated by a delimiter, usually a comma (`,`).

  • Ensure the file is accessible to JMeter (e.g., in the same directory as your .jmx script).
  • You don't need a header row in the CSV file itself; you'll define variable names in JMeter.

CSV File Example

Here's what a simple CSV file might look like, containing usernames and passwords:

user1,pass1
user2,pass2
user3,pass3

Each line provides a unique pair of username and password that can be used by a different virtual user or in different test iterations.

Adding CSV Data Set Config

To add this element in JMeter:

  1. Right-click on your Test Plan or Thread Group.
  2. Go to Add > Config Element > CSV Data Set Config.

Placing it under the Test Plan makes it available globally. Placing it under a Thread Group limits its scope to that specific group.

Configuring Key Fields

The CSV Data Set Config has several important fields to configure:

  • Filename: Path to your CSV file.
  • Variable Names: Comma-separated list of names for variables (e.g., username,password).
  • Delimiter: Character separating values (default is comma).
  • Recycle on EOF?: If True, JMeter restarts from the beginning of the file when it reaches the end.
  • Stop Thread on EOF?: If True, the thread stops when it runs out of data.
  • Sharing Mode: How data is shared among threads (e.g., All threads, Current thread group).

Using Variables in Requests

Once you've defined your variable names in the CSV Data Set Config (e.g., username and password), you can use them in your JMeter Samplers.

The syntax for referencing a variable is ${variableName}.

For instance, in an HTTP Request, you might use ${username} in the username field and ${password} in the password field.

Example: Login Request

Let's say you're testing a login page that accepts user and pass as parameters. With a CSV file like user1,pass1, you'd configure your HTTP Request as follows:

Method: POST
Path: /login
Parameters:
  user: ${username}
  pass: ${password}

Each time a virtual user makes this request, JMeter will pull the next available username and password from your CSV file.

CSV Configuration Check

You've successfully set up a CSV Data Set Config. Which of the following are crucial settings you must define for it to work correctly?

Recap: Parameterizing with CSV

Great job! You've learned how to parameterize your JMeter tests using the CSV Data Set Config.

  • Parameterization makes tests more realistic.
  • The CSV Data Set Config reads data from a CSV file.
  • You define variable names in the Config element to map to your CSV columns.
  • Use the ${variableName} syntax to reference data in your samplers.
  • Key settings include Filename, Variable Names, Delimiter, Recycle on EOF, Stop Thread on EOF, and Sharing Mode.

Next, we'll explore handling dynamic values directly from server responses!

常见问题解答

「使用 CSV 数据进行参数化」课时是免费的吗?

是的 — 「使用 CSV 数据进行参数化」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Load Testing & Performance Benchmarking (JMeter & k6) 课程的其余内容,请升级到 CoddyKit PRO。 Load Testing & Performance Benchmarking (JMeter & k6) 课程共包含 4 节课。

「使用 CSV 数据进行参数化」这节课中我会学到什么?

使用 CSV 数据集配置将动态输入数据注入测试脚本 你通过在浏览器中直接运行的动手代码来练习 Load Testing & Performance Benchmarking (JMeter & k6),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Load Testing & Performance Benchmarking (JMeter & k6) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Load Testing & Performance Benchmarking (JMeter & k6) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「使用 CSV 数据进行参数化」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Load Testing & Performance Benchmarking (JMeter & k6) 课中编写并运行代码吗?

能。每节 Load Testing & Performance Benchmarking (JMeter & k6) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 CSV 数据进行参数化
  2. 处理动态值(关联)
  3. JMeter 函数与变量
  4. 使用 JDBC 进行数据库测试
← 返回 Load Testing & Performance Benchmarking (JMeter & k6)