Skip to content

🔧 后置处理器

后置处理器是 Ryze 框架中在测试组件执行后运行的特殊组件,用于执行后处理逻辑,如数据清理、结果验证、通知发送等。

🔍 基本概念

作用原理

后置处理器在测试组件(取样器、测试集合等)执行后自动运行,可以执行各种后处理逻辑,如清理测试数据、验证结果、发送通知等。

配置位置

后置处理器可以配置在:

  • 测试集合中:在测试集合执行后运行
  • 取样器中:在取样器执行后运行
  • 模块级集合:在模块测试后运行
  • 项目级集合:在整个项目测试后运行

🎯 设计理念

测试清理自动化

后置处理器的设计理念是实现测试清理和结果处理的自动化:

  • 数据清理:自动清理测试产生的临时数据
  • 结果验证:对测试结果进行额外的验证
  • 状态恢复:将测试环境恢复到初始状态
  • 通知发送:发送测试结果通知

协议无关性

后置处理器设计为协议无关的组件,不同协议的后置处理器具有相似的结构和使用方式:

yaml
# 通用后置处理器结构
- testclass: 协议类型
  config:
  # 协议特定配置项

⏱️ 执行时机

执行顺序

后置处理器遵循严格的执行顺序(与前置处理器相反):

  1. 取样器级后置处理器:最先执行
  2. 集合级后置处理器:其次执行
  3. 模块级后置处理器:然后执行
  4. 项目级后置处理器:最后执行

执行上下文

后置处理器在执行时具有以下特点:

  • 变量访问:可以访问测试过程中生成的变量
  • 结果访问:可以访问测试组件的执行结果
  • 配置继承:可以继承配置元件的配置
yaml
title: 测试集合
variables:
  user_id: 12345

children:
  - title: 用户操作测试
    children:
      - testclass: http
        config:
          method: POST
          path: /users
          body:
            name: Test User
        extractors:
          # 提取新创建的用户ID
          - { testclass: json, field: '$.data.id', ref_name: new_user_id }
        # 取样器级后置处理器
        postprocessors:
          - testclass: http
            config:
              method: POST
              path: /notifications
              body:
                message: "User ${new_user_id} created"
    postprocessors:
      - testclass: http
        config:
          method: DELETE
          path: /users/${new_user_id}
        # 清理测试数据
postprocessors:
  - testclass: jdbc
    config:
      sql: "DELETE FROM audit_logs WHERE user_id = ${user_id}"

🌐 各协议后置处理器

🌐 HTTP 后置处理器

用于执行 HTTP 请求作为后处理逻辑。

配置模板

yaml
- testclass: http
  config:
    method: post
    protocol: http
    http/2: false
    port: 8080
    host: localhost
    path: /user
    headers:
      h1: 1
    query: { }
    data: { }
    body: { userName: 'ryze', password: '123456qq' }

相关文档HTTP 协议文档

🗄️ JDBC 后置处理器

用于执行数据库操作作为后处理逻辑。

配置模板

yaml
- testclass: jdbc
  config:
    sql: "DELETE FROM users WHERE name = 'testuser'"

相关文档JDBC 协议文档

🗃️ Redis 后置处理器

用于执行 Redis 操作作为后处理逻辑。

配置模板

yaml
- testclass: redis
  config:
    command: DEL
    key: test_key

相关文档Redis 协议文档

🔌 Dubbo 后置处理器

用于执行 Dubbo 服务调用作为后处理逻辑。

配置模板

yaml
- testclass: dubbo
  config:
    interface: com.example.UserService
    method: deleteUser
    parameters:
      - 12345

相关文档Dubbo 协议文档

🚀 Kafka 后置处理器

用于发送 Kafka 消息作为后处理逻辑。

配置模板

yaml
- testclass: kafka
  config:
    topic: user-events
    key: user-deleted
    value: '{"userId": 12345, "action": "delete"}'

相关文档Kafka 协议文档

🐰 RabbitMQ 后置处理器

用于发送 RabbitMQ 消息作为后处理逻辑。

配置模板

yaml
- testclass: rabbit
  config:
    exchange: user-exchange
    routing_key: user.deleted
    message: '{"userId": 12345, "timestamp": "${timestamp()}"}'

相关文档RabbitMQ 协议文档

🎯 ActiveMQ 后置处理器

用于发送 ActiveMQ 消息作为后处理逻辑。

配置模板

yaml
- testclass: active
  config:
    destination: user-queue
    message: '{"userId": 12345, "action": "delete"}'

相关文档ActiveMQ 协议文档

🍃 MongoDB 后置处理器

用于执行 MongoDB 操作作为后处理逻辑。

配置模板

yaml
- testclass: mongo
  config:
    operation: delete
    collection: users
    filter: '{"name": "testuser"}'

相关文档MongoDB 协议文档

🔧 使用场景

测试数据清理

在测试后清理产生的测试数据:

yaml
title: 用户管理测试
children:
  - testclass: http
    config:
      method: POST
      path: /users
      body:
        name: Test User
        email: test@example.com
    extractors:
      - { testclass: json, field: '$.data.id', ref_name: user_id }
    postprocessors:
      # 测试后清理创建的用户
      - testclass: http
        config:
          method: DELETE
          path: /users/${user_id}

postprocessors:
  # 集合级清理:清理所有测试用户
  - testclass: jdbc
    config:
      sql: "DELETE FROM users WHERE name LIKE 'Test User%'"

结果验证和报告

在测试后进行额外的结果验证和报告生成:

yaml
title: 订单处理测试
children:
  - testclass: http
    config:
      method: POST
      path: /orders
      body:
        product_id: 12345
        quantity: 2
    extractors:
      - { testclass: json, field: '$.data.order_id', ref_name: order_id }
    postprocessors:
      # 验证订单状态
      - testclass: http
        config:
          method: GET
          path: /orders/${order_id}
        assertions:
          - { testclass: json, field: '$.data.status', expected: 'processed', rule: '==' }

      # 发送测试结果通知
      - testclass: http
        config:
          method: POST
          path: /notifications
          body:
            type: 'test_result'
            message: "Order ${order_id} processed successfully"

状态恢复

在测试后将系统状态恢复到初始状态:

yaml
title: 库存管理测试
preprocessors:
  # 保存初始库存状态
  - testclass: jdbc
    config:
      sql: "SELECT quantity FROM inventory WHERE product_id = 'PRODUCT_001'"
    extractors:
      - { testclass: result, ref_name: initial_quantity }

children:
  - testclass: http
    config:
      method: PUT
      path: /inventory/PRODUCT_001
      body:
        quantity: 0  # 扣减库存到0

postprocessors:
  # 恢复初始库存状态
  - testclass: jdbc
    config:
      sql: "UPDATE inventory SET quantity = ${initial_quantity} WHERE product_id = 'PRODUCT_001'"

日志记录和审计

在测试后记录操作日志和审计信息:

yaml
title: 用户权限测试
variables:
  test_start_time: ${timestamp()}

children:
  - testclass: http
    config:
      method: PUT
      path: /users/${user_id}/permissions
      body:
        role: admin
    postprocessors:
      # 记录权限变更日志
      - testclass: jdbc
        config:
          sql: |
            INSERT INTO audit_logs (user_id, action, timestamp, details) 
            VALUES (?, ?, ?, ?)
          parameters:
            - ${user_id}
            - 'permission_change'
            - ${timestamp()}
            - 'Changed role to admin'

postprocessors:
  # 记录测试完成日志
  - testclass: jdbc
    config:
      sql: |
        INSERT INTO test_logs (test_name, start_time, end_time, status) 
        VALUES (?, ?, ?, ?)
      parameters:
        - "User Permission Test"
        - ${test_start_time}
        - ${timestamp()}
        - "completed"

📚 最佳实践

处理器分层原则

项目级处理器

  • 全局清理:整个项目的通用清理逻辑
  • 报告生成:生成项目级的测试报告
  • 状态重置:将整个测试环境重置到初始状态

模块级处理器

  • 模块清理:特定模块的清理工作
  • 数据归档:归档模块的测试数据
  • 服务重置:重置模块依赖的服务状态

用例级处理器

  • 用例清理:当前用例特定的清理工作
  • 结果记录:记录用例的执行结果
  • 资源释放:释放用例使用的资源

错误处理

异常容忍

后置处理器通常应该容忍异常,确保即使后置处理失败也不会影响测试结果:

yaml
postprocessors:
  - testclass: http
    config:
      method: DELETE
      path: /users/${user_id}
    # 即使清理失败,也不影响测试结果
    ignore_errors: true

条件执行

根据测试结果决定是否执行后置处理:

yaml
postprocessors:
  - testclass: http
    # 仅在测试成功时执行清理
    condition: ${test_result == 'success'}
    config:
      method: DELETE
      path: /users/${user_id}

  - testclass: http
    # 仅在测试失败时发送告警
    condition: ${test_result == 'failed'}
    config:
      method: POST
      path: /alerts
      body:
        message: "Test failed, manual cleanup required"

性能优化

异步处理

对于不影响测试结果的后置处理,可以考虑异步执行:

yaml
postprocessors:
  - testclass: http
    async: true  # 异步执行
    config:
      method: POST
      path: /reports
      body:
        test_name: "User Management Test"
        duration: ${test_duration}

批量操作

将多个相似的后置处理合并为批量操作:

yaml
postprocessors:
  - testclass: jdbc
    config:
      sql: |
        DELETE FROM users WHERE id IN (${user_ids});
        DELETE FROM orders WHERE user_id IN (${user_ids});
        DELETE FROM audit_logs WHERE user_id IN (${user_ids});

可维护性

配置分离

将复杂的后置处理配置分离到独立的配置文件中:

yaml
# 在单独的配置文件中定义复杂的处理器
postprocessors:
  - !include 'processors/cleanup_test_data.yaml'
  - !include 'processors/generate_test_report.yaml'

模块化设计

将相关的后置处理逻辑组织成模块:

yaml
postprocessors:
  # 数据清理模块
  - !include 'processors/data_cleanup.yaml'
  # 报告生成模块
  - !include 'processors/report_generation.yaml'

💡 提示:后置处理器是确保测试环境清洁和结果可靠的重要工具,合理使用可以显著提高测试的自动化程度!

Released under the MIT License.