Skip to content

固定和模拟数据(Pinning and mocking data)#

在开发工作流时,你可能希望测试你的逻辑,而不必反复调用外部系统或处理实时数据。n8n 提供了两个相关功能来帮助实现这一点:

🌐 When developing workflows, you might want to test your logic without repeatedly calling external systems or working with live data. n8n provides two related features to help with this:

  • 数据模拟:在不连接真实数据源的情况下创建或模拟测试数据
  • 数据固定:保存测试数据(模拟或真实数据),并在未来的工作流执行中重复使用,而不是重新获取新数据

这两种方法在开发过程中都能节省时间和资源,帮助你使用一致的数据集,并保护实时系统免受反复测试调用的影响。

🌐 Both approaches save time and resources during development, help you work with consistent datasets, and protect live systems from repeated test calls.

仅用于开发

数据固定和模拟是帮助在开发过程中测试工作流的功能。数据固定在生产工作流执行中不可用。

数据模拟方法(Data mocking approaches)#

创建测试数据以在开发过程中使用。你可以通过几种方式创建模拟数据:

🌐 Create test data to work with during development. You can create mock data in several ways:

使用“代码”或“编辑字段”节点生成自定义数据(Generate custom data using the Code or Edit Fields nodes)#

你可以在工作流中使用代码节点编辑字段(设置)节点来创建自定义数据集。

🌐 You can create a custom dataset in your workflow using either the Code node or the Edit Fields (Set) node.

在代码节点中,你可以创建任何你想要的数据集,并将其作为节点输出返回。在编辑字段节点中,选择 添加字段 来添加你的自定义数据。

🌐 In the Code node, you can create any data set you want, and return it as the node output. In the Edit Fields node, select Add fields to add your custom data.

编辑字段节点是进行小型测试的不错选择。要创建更复杂的数据集,请使用代码节点。

🌐 The Edit Fields node is a good choice for small tests. To create more complex datasets, use the Code node.

使用此方法的情况:当你需要完全控制测试数据的结构和数值,或当你想使用特定的数据模式测试边界情况时。

从客户数据存储节点输出示例数据集(Output a sample data set from the Customer Datastore node)#

客户数据存储节点提供了一个可用的虚拟数据集。添加并执行该节点以探索数据。

🌐 The Customer Datastore node provides a fake dataset to work with. Add and execute the node to explore the data.

在以下情况下使用此方法:当你在探索 n8n 时需要一些测试数据,但没有实际用例可用时。

一旦你创建或获得了希望在多个工作流执行中重复使用的测试数据,请使用数据固定来保存它,以进行一致的测试。

🌐 Once you've created or obtained test data you want to reuse across multiple workflow executions, use Data pinning to save it for consistent testing.

数据绑定(Data pinning)#

在工作流开发过程中,你可以“固定”数据。数据固定意味着保存节点的输出数据,在以后的工作流执行中使用已保存的数据,而不是获取新的数据。

🌐 You can 'pin' data during workflow development. Data pinning means saving the output data of a node and using the saved data instead of fetching fresh data in future workflow executions.

当处理来自外部来源的数据时,你可以使用此方法,以避免重复向外部系统发送请求。这可以节省时间和资源:

🌐 You can use this when working with data from external sources to avoid having to repeat requests to the external system. This can save time and resources:

  • 如果你的工作流依赖外部系统(例如 Webhook 调用)来触发,则能够固定数据意味着你无需在每次测试工作流时都使用该外部系统。
  • 如果外部资源有数据或使用量限制,在测试期间锁定数据可以避免占用你的资源限制。
  • 你可以获取并锁定要测试的数据,从而确保所有工作流测试中的数据一致。
  • 你可以模拟测试数据(使用上述方法),然后将其固定以便在多次执行中重用。

你只能为具有单个主要输出的节点固定数据(“错误”输出不计入此目的)。

🌐 You can only pin data for nodes that have a single main output ("error" outputs don't count for this purpose).

Pin 数据(Pin data)#

To pin data in a node:

  1. Run the node to load data.
  2. In the OUTPUT view, select Pin data Pin data icon. When data pinning is active, the button is disabled and a "This data is pinned" banner is displayed in the OUTPUT view.

Nodes that output binary data

You can't pin data if the output data includes binary data.

取消置顶数据(Unpin data)#

当数据固定功能处于激活状态时,节点输出面板顶部会出现一个横幅,提示 n8n 已固定数据。要取消数据固定并在下一次执行时获取最新数据,请在横幅中选择 取消固定 链接。

🌐 When data pinning is active, a banner appears at the top of the node's output panel indicating that n8n has pinned the data. To unpin data and fetch fresh data on the next execution, select the Unpin link in the banner.

编辑固定数据(Edit pinned data)#

n8n 允许你编辑固定数据。这意味着你可以在不设置每个场景并从外部系统发送相关数据的情况下检查不同的场景。它使测试边缘情况变得更容易。

🌐 n8n allows you to edit pinned data. This means you can check different scenarios without setting up each scenario and sending the relevant data from your external system. It makes it easier to test edge cases.

仅用于开发

数据编辑在生产工作流执行中不可用。这是一个用于帮助在开发过程中测试工作流的功能。

编辑输出数据(Edit output data)#

编辑输出数据:

🌐 To edit output data:

  1. 运行节点以加载数据。
  2. 输出 视图中,选择 JSON 以切换到 JSON 视图。
  3. 选择 编辑 编辑数据图标
  4. 编辑你的数据。
  5. 选择 保存。n8n 会保存你的数据更改并固定你的数据。

使用先前执行的数据(Use data from previous executions)#

你可以从先前工作流执行中的节点复制数据:

🌐 You can copy data from nodes in previous workflow executions:

  1. 打开左侧菜单。
  2. 选择 执行
  3. 浏览工作流执行列表以找到包含要复制的数据的工作流。
  4. 选择 打开过去的执行 打开过去的执行图标
  5. 双击要复制其数据的节点。
  6. 如果是表格布局,选择 JSON 以切换到 JSON 视图。
  7. 你可以通过两种方式复制 JSON:
  8. 通过高亮显示来选择你想要的 JSON,就像选择文本一样。然后使用 ctrl + c 来复制它。
  9. 通过点击一个参数选择你想要复制的 JSON。然后:
    1. 将鼠标悬停在 JSON 上。n8n 会显示 复制 复制数据图标 按钮。
    2. 选择 复制 复制数据图标
    3. 你可以选择要复制的内容:
      • 复制项目路径复制参数路径为你提供访问 JSON 部分的表达式。
      • 复制值:复制所选的整个 JSON。
  10. 返回到你正在处理的工作流:
    1. 打开左侧菜单。
    2. 选择 工作流
    3. 选择 打开
    4. 选择要打开的工作流。
  11. 打开你想使用复制数据的节点。10. 如果没有数据,请运行节点以加载数据。11. 在 输出(OUTPUT) 视图中,选择 JSON 以切换到 JSON 视图。12. 选择 编辑(Edit) 编辑数据图标。15. 粘贴上一次执行的数据。16. 选择 保存(Save)。n8n 会保存你的数据更改并固定数据。

将模拟与固定结合(Combine mocking with pinning)#

为了获得最真实的测试体验,你可以结合模拟和固定方法:

🌐 For the most realistic testing experience, you can combine mocking and pinning approaches:

  1. 使用其中一种模拟方法(代码节点、编辑字段节点或客户数据存储)创建测试数据
  2. 编辑测试数据以创建特定的测试场景或边界情况
  3. 固定已编辑的数据以便在多个工作流执行中重用
  4. 继续使用这个已编辑、固定的数据集进行开发

这种方法让你完全控制测试数据,同时确保多次运行中的测试一致性。

🌐 This approach gives you complete control over your test data while ensuring consistent testing across multiple runs.