---
title: "管理触发测试方法 - 门卫触发"
language: "zh"
url: "https://docs.testany.io/zh/docs/managing-test-gatekeeper/"
markdown_url: "https://docs.testany.io/zh/docs/managing-test-gatekeeper.md"
translation: "https://docs.testany.io/en/docs/managing-test-gatekeeper.md"
last_updated: "2026-10-22T12:44:01Z"
---

# 管理触发测试方法 - 门卫触发

### 门卫介绍

在动态的软件开发中，效率和响应性至关重要。Testany平台中的"门卫"功能为用户提供了通过Webhook触发特定测试组的能力。

[视频](https://customer-en3hmhs3okg5hi37.cloudflarestream.com/278ede0abc85cd7a9ba2d487383168d8/watch)

### 门卫定义及其关键组成部分

#### 功能概述

门卫允许用户在特定事件发生时触发预定义的测试管道组，从而提升产品优化和问题监控。此功能对旨在提高发布和部署质量的产品团队和运维团队都非常有益。通过与监控平台（如Splunk或Dynatrace）的告警信号结合使用，门卫可显著提高故障排查效率，甚至在事件发生之前就开始处理。

#### 门卫密钥

每个门卫都分配有一个唯一标识符，即“门卫密钥”。门卫密钥格式为 `G-{workspace_key}-{5位大写十六进制}`（例如 `G-WKS-01AD1`），用于标识门卫及其关联的工作区。

#### Webhook在门卫中的应用

门卫的功能通过Webhook得到增强。这些Webhook对于触发测试管道的执行至关重要。用户可以配置这些Webhook以响应各种事件，从而根据定义的条件自动执行测试。这一集成对于保持高效且响应迅速的测试环境至关重要，能够快速适应操作生态系统中的变化和新数据。

#### 门卫所有者的角色

门卫所有者负责编辑和管理门卫，定义触发测试的条件，并确保相关团队成员得到通知。

### 设置门卫

创建门卫的步骤如下：

1. **创建门卫**：

   - 如果您是工作区成员，您将在左侧栏看到一个新的标签“门卫”。点击它将进入门卫部分。

     ![image-20240113-092126.png](https://docs.testany.io/docs/images/zh/image-20240113-092126_fbb70733.png)
   - 点击右上角的“创建门卫”按钮，会弹出一个创建向导：

     ![image-20240113-092434.png](https://docs.testany.io/docs/images/zh/image-20240113-092434_283b26af.png)
     - 输入门卫的名称及描述（可选），然后点击下一步。至此，您已创建了一个新的门卫。
2. **配置Webhook和测试管道**：

   - 进入门卫详情页面进一步编辑门卫。

     ![image-20240113-103653.png](https://docs.testany.io/docs/images/zh/image-20240113-103653_06f22c23.png)
   - 门卫详情页面是一个内联编辑页面。拥有编辑权限的用户可以通过点击编辑并在失去焦点时保存的方法来编辑页面中的所有字段，除了门卫密钥。

> 请注意：触发方式、触发名称和触发条件字段非常重要，请尽可能准确地填写，以便您和其他用户理解您设置的门卫。

3. **定义触发条件**：门卫所有者设置测试执行的条件。

### 管理门卫

门卫可以通过以下方式轻松管理：

1. **编辑和更新门卫**：根据需要更改配置。
2. **添加用户至观察者列表**：确保相关团队成员保持知情。
3. **转移所有权**：将所有权转移给其他工作区成员，以确保门卫得到良好维护。

### 使用门卫的最佳实践

门卫的核心价值在于：**当外部系统发生特定事件时，通过 Webhook 自动触发 Testany 上的测试执行**。以下是五个典型的集成场景。

#### 场景 1：CI/CD 流水线集成（以 Jenkins 为例）

**适用场景**：在代码部署到测试环境或生产环境后，自动触发冒烟测试或回归测试，确保部署质量。

**配置步骤**：

1. 在 Testany 中创建门卫，并获取 Webhook URL（参见上文"设置门卫"章节）。

2. 在 Jenkins Pipeline 脚本中添加 HTTP 请求步骤，调用门卫的 Webhook URL。

   使用 [HTTP Request 插件](https://plugins.jenkins.io/http_request/)：

   ```groovy
   pipeline {
       agent any
       stages {
           stage('Deploy') {
               steps {
                   // 您的部署步骤
                   sh 'deploy.sh'
               }
           }
           stage('Trigger Testany Tests') {
               steps {
                   httpRequest(
                       url: 'https://app.testany.com.cn/api/v2/gatekeeper/webhook/YOUR_GATEKEEPER_TOKEN',
                       httpMode: 'POST',
                       contentType: 'APPLICATION_JSON',
                       requestBody: '{"source": "jenkins", "environment": "staging"}'
                   )
               }
           }
       }
   }
   ```

   或使用 `curl` 命令：

   ```groovy
   stage('Trigger Testany Tests') {
       steps {
           sh '''
               curl -X POST "https://app.testany.com.cn/api/v2/gatekeeper/webhook/YOUR_GATEKEEPER_TOKEN" \
                    -H "Content-Type: application/json" \
                    -d '{"source": "jenkins", "environment": "staging"}'
           '''
       }
   }
   ```

**其他 CI/CD 平台**：

此方法同样适用于其他 CI/CD 平台，只需在流水线中添加 HTTP POST 请求即可：
- **GitHub Actions**：使用 [curl](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions) 或 [HTTP Request Action](https://github.com/marketplace/actions/http-request-action)
- **GitLab CI**：在 `.gitlab-ci.yml` 中使用 `curl` 命令
- **Azure DevOps**：使用 [InvokeRESTAPI 任务](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/invoke-rest-api-v1)

#### 场景 2：监控系统集成（以 Splunk 为例）

**适用场景**：当监控系统检测到异常指标（如错误率上升、响应时间变长）时，自动触发相关功能的诊断测试，帮助快速定位问题。

**配置步骤**：

1. 登录 Splunk，创建您的业务查询。

   ![image-20240114-125102.png](https://docs.testany.io/docs/images/zh/image-20240114-125102_42b8936f.png)

2. 将搜索保存为告警。

   ![image-20240114-125201.png](https://docs.testany.io/docs/images/zh/image-20240114-125201_27ffde9c.png)

3. 配置告警条件：设置告警触发的阈值、时间范围和严重性级别。

   ![image-20240114-125634.png](https://docs.testany.io/docs/images/zh/image-20240114-125634_1b1f0845.png)

4. 设置 Webhook：点击"添加操作"，选择"Webhook"。

   ![image-20240114-130138.png](https://docs.testany.io/docs/images/zh/image-20240114-130138_3fa6b20b.png)

   粘贴从 Testany 门卫获取的 Webhook URL：

   ![image-20240114-130851.png](https://docs.testany.io/docs/images/zh/image-20240114-130851_adf8c79b.png)
   ![image-20240114-130617.png](https://docs.testany.io/docs/images/zh/image-20240114-130617_67ad4f62.png)

5. 保存并启用告警：点击"保存"，然后点击"查看告警"。

   ![image-20240114-131240.png](https://docs.testany.io/docs/images/zh/image-20240114-131240_6173abe5.png)
   ![image-20240114-131420.png](https://docs.testany.io/docs/images/zh/image-20240114-131420_81dca5cb.png)

**其他监控平台**：

- **Datadog**：参考 [Webhooks Integration](https://docs.datadoghq.com/integrations/webhooks/)
- **Dynatrace**：参考 [Problem Notifications - Webhook](https://docs.dynatrace.com/docs/observe-and-explore/notifications-and-alerting/problem-notifications/webhook-integration)
- **Prometheus + Alertmanager**：参考 [Webhook Config](https://prometheus.io/docs/alerting/latest/configuration/#webhook_config)

#### 场景 3：事件管理系统集成（以 PagerDuty 为例）

**适用场景**：当 On-Call 工程师收到告警时，自动运行诊断测试来帮助定位问题根因，加速故障排查。

**配置步骤**：

1. 在 Testany 中创建门卫，配置用于故障诊断的测试管道。

2. 登录 [PagerDuty](https://www.pagerduty.com/)，进入 **Services** → 选择目标服务 → **Integrations** 标签。

3. 点击 **Add an extension**，选择 **Generic V2 Webhook**。

4. 配置 Webhook：
   - **Name**：填写扩展名称，如 "Testany Gatekeeper"
   - **URL**：粘贴 Testany 门卫的 Webhook URL
   - **Events**：选择触发事件类型（如 `incident.triggered`）

5. 点击 **Save** 保存配置。

配置完成后，当该服务触发告警时，PagerDuty 会自动调用门卫 Webhook，触发 Testany 上的诊断测试。

更多详情请参考 [PagerDuty Webhooks 文档](https://docs.pagerduty.com/developer/webhooks-overview)。

**其他事件管理平台**：

- **Opsgenie**：参考 [Webhook Integration](https://support.atlassian.com/opsgenie/docs/integrate-opsgenie-with-webhook/)
- **VictorOps (Splunk On-Call)**：参考 [Outgoing Webhooks](https://help.victorops.com/knowledge-base/rest-endpoint-integration-guide/)

#### 场景 4：ChatOps 集成（以 Slack 为例）

**适用场景**：让团队成员通过 Slack 命令快速触发测试，降低使用门槛，无需登录 Testany 平台即可发起测试。

**配置步骤**：

1. 在 Testany 中创建门卫，获取 Webhook URL。

2. 创建 Slack App：
   - 访问 [Slack API](https://api.slack.com/apps)，点击 **Create New App**
   - 选择 **From scratch**，填写 App 名称并选择工作区

3. 配置 Slash Command：
   - 在 App 设置中，进入 **Slash Commands** → **Create New Command**
   - **Command**：填写命令名称，如 `/testany-smoke`
   - **Request URL**：粘贴 Testany 门卫的 Webhook URL
   - **Short Description**：填写命令描述，如 "触发冒烟测试"

4. 安装 App 到工作区：进入 **Install App**，点击 **Install to Workspace**。

配置完成后，团队成员在 Slack 中输入 `/testany-smoke` 即可触发对应的测试管道。

更多详情请参考 [Slack Slash Commands 文档](https://api.slack.com/interactivity/slash-commands)。

**其他 ChatOps 平台**：

- **Microsoft Teams**：参考 [Outgoing Webhooks](https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-outgoing-webhook)
- **飞书**：参考 [机器人发送消息](https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot)

#### 场景 5：第三方服务依赖监控（以 Statuspage 为例）

**适用场景**：当您依赖的第三方服务（支付网关、短信服务、云存储等）状态发生变化时，自动触发集成测试验证对您系统的影响。

**配置步骤**：

1. 在 Testany 中创建门卫，配置与该第三方服务相关的集成测试管道。

2. 登录 [Atlassian Statuspage](https://www.atlassian.com/software/statuspage)（或您依赖的服务的 Statuspage）。

3. 进入 **Subscribers** → **Webhook Subscribers** → **Add Subscriber**。

4. 配置 Webhook：
   - **Endpoint URL**：粘贴 Testany 门卫的 Webhook URL
   - **Email**：填写通知邮箱（用于 Webhook 失败时的通知）
   - **Components**：选择要监控的组件

5. 点击 **Save** 保存配置。

配置完成后，当第三方服务状态发生变化（如从 Operational 变为 Degraded Performance），Statuspage 会自动调用门卫 Webhook，触发相关的集成测试。

更多详情请参考 [Statuspage Webhook Notifications 文档](https://support.atlassian.com/statuspage/docs/enable-webhook-notifications/)。

**常见的第三方服务 Statuspage**：

- [AWS Status](https://health.aws.amazon.com/health/status)
- [Stripe Status](https://status.stripe.com/)
- [Twilio Status](https://status.twilio.com/)
- [GitHub Status](https://www.githubstatus.com/)

### 结论

门卫是Testany平台中的一项强大工具，能够简化和优化测试管理。通过有效利用其功能，团队可以显著提升测试流程和产品质量。

×

## 本页术语

- [门卫 (Gatekeeper)](https://docs.testany.io/zh/docs/glossary.md#gatekeeper)
- [用户 (User)](https://docs.testany.io/zh/docs/glossary.md#user)
- [工作区 (Workspace)](https://docs.testany.io/zh/docs/glossary.md#workspace)
- [执行 (Execution)](https://docs.testany.io/zh/docs/glossary.md#execution)
- [环境 (Environment)](https://docs.testany.io/zh/docs/glossary.md#environment)
- [门卫所有者 (Gatekeeper Owner)](https://docs.testany.io/zh/docs/glossary.md#gatekeeper-owner)
- [工作区成员 (Workspace Member)](https://docs.testany.io/zh/docs/glossary.md#workspace-member)
- [测试执行 (Test Execution)](https://docs.testany.io/zh/docs/glossary.md#test-execution)
- [观察者 (Watcher)](https://docs.testany.io/zh/docs/glossary.md#watcher)
- [命令 (Command)](https://docs.testany.io/zh/docs/glossary.md#command)
