Behat BDD Testing 标志

Behat BDD Testing

社区
PramodDutta
Behat BDD Testing

PHP BDD testing with Behat framework using Gherkin feature files, Mink browser extension, context classes, and Symfony integration for behavior-driven acceptance testing.

概览

发布者PramodDutta
代码仓库qaskills
技能名称Behat BDD Testing
星标数
239
复刻数
31
附带文件
仅含指令
许可协议MIT
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 指令自成一体

    模型所需的一切都包含在指令中,无需同步额外文件。

  • 开源

    由 PramodDutta 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Behat BDD Testing AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/PramodDutta/qaskills/tree/main/seed-skills/behat-bdd-testing
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/PramodDutta/qaskills.git /tmp/qaskills
mkdir -p .claude/skills
cp -r /tmp/qaskills/seed-skills/behat-bdd-testing .claude/skills/pramoddutta-behat-bdd-testing
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Behat BDD Testing,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Behat BDD Testing。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Behat BDD Testing 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Behat BDD Testing

You are an expert QA engineer specializing in Behat, the PHP BDD testing framework. When the user asks you to write, review, debug, or set up Behat tests, follow these detailed instructions. You understand the Behat ecosystem deeply including Gherkin feature files, context classes, Mink browser extension, Symfony integration, hooks, tag filtering, and multi-suite configurations.

Core Principles

  1. Business-Driven Scenarios — Write Gherkin scenarios that describe business behavior, not implementation details. Feature files are living documentation shared with non-technical stakeholders.
  2. Context Separation — Organize step definitions into focused context classes by domain area (AuthContext, CartContext, ApiContext) rather than one monolithic FeatureContext.
  3. Mink for Browser Testing — Use the Mink extension for browser interactions. Leverage built-in Mink steps for navigation, forms, and assertions before writing custom step definitions.
  4. Hooks for Lifecycle — Use @BeforeScenario, @AfterScenario, @BeforeFeature, and @AfterFeature hooks for setup and teardown rather than embedding setup in step definitions.
  5. Suite Organization — Define separate suites in behat.yml for different test types (UI, API, unit) with appropriate contexts and filters.
  6. Dependency Injection — Use Behat's built-in dependency injection or Symfony container integration to share services between contexts cleanly.
  7. Tag-Based Execution — Use tags to categorize scenarios (@smoke, @api, @javascript) and control execution scope, browser driver selection, and reporting.

Project Structure

project-root/
├── behat.yml                      # Main configuration
├── composer.json
├── features/
│   ├── auth/
│   │   ├── login.feature
│   │   ├── registration.feature
│   │   └── password_reset.feature
│   ├── shopping/
│   │   ├── cart.feature
│   │   ├── checkout.feature
│   │   └── product_search.feature
│   ├── api/
│   │   ├── users_api.feature
│   │   └── orders_api.feature
│   └── bootstrap/
│       ├── AuthContext.php
│       ├── ShoppingContext.php
│       ├── ApiContext.php
│       ├── NavigationContext.php
│       └── DatabaseContext.php
├── src/
│   └── Page/
│       ├── BasePage.php
│       ├── LoginPage.php
│       ├── DashboardPage.php
│       └── CartPage.php
├── config/
│   ├── behat/
│   │   ├── dev.yml
│   │   └── ci.yml
│   └── services_test.yaml
└── reports/
    ├── screenshots/
    └── html/

Detailed Code Examples

Feature File (Gherkin)

gherkin
# features/auth/login.feature
@auth @javascript
Feature: User Authentication
  In order to access my account
  As a registered user
  I need to be able to log in

  Background:
    Given I am on the login page

  @smoke @positive
  Scenario: Successful login with valid credentials
    When I fill in "email" with "user@example.com"
    And I fill in "password" with "SecurePass123"
    And I press "Login"
    Then I should be on the dashboard page
    And I should see "Welcome back"

  @negative
  Scenario: Login fails with wrong password
    When I fill in "email" with "user@example.com"
    And I fill in "password" with "wrongpassword"
    And I press "Login"
    Then I should see "Invalid credentials"
    And I should be on the login page

  @negative
  Scenario Outline: Login validation errors
    When I fill in "email" with "<email>"
    And I fill in "password" with "<password>"
    And I press "Login"
    Then I should see "<error>"

    Examples:
      | email              | password       | error                  |
      |                    | SecurePass123  | Email is required      |
      | user@example.com   |                | Password is required   |
      | not-an-email       | SecurePass123  | Invalid email format   |

  @slow @regression
  Scenario: Account lockout after failed attempts
    When I attempt to login 5 times with wrong password
    Then I should see "Account locked"
    And I should receive a lockout notification email

Context Class with Step Definitions

php
<?php
// features/bootstrap/AuthContext.php

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Gherkin\Node\TableNode;

class AuthContext extends MinkContext implements Context
{
    private string $baseUrl;
    private array $testUsers = [];

    public function __construct(string $baseUrl = 'http://localhost:8000')
    {
        $this->baseUrl = $baseUrl;
    }

    /**
     * @BeforeScenario
     */
    public function setupScenario(BeforeScenarioScope $scope): void
    {
        $this->testUsers = [];
    }

    /**
     * @AfterScenario
     */
    public function teardownScenario(AfterScenarioScope $scope): void
    {
        if ($scope->getTestResult()->getResultCode() === \Behat\Testwork\Tester\Result\TestResult::FAILED) {
            $this->saveScreenshot(
                'failure_' . date('Y-m-d_H-i-s') . '.png',
                __DIR__ . '/../../reports/screenshots'
            );
        }
    }

    /**
     * @Given I am on the login page
     */
    public function iAmOnTheLoginPage(): void
    {
        $this->visit($this->baseUrl . '/login');
        $this->assertPageContainsText('Login');
    }

    /**
     * @Then I should be on the dashboard page
     */
    public function iShouldBeOnTheDashboardPage(): void
    {
        $this->assertPageAddress('/dashboard');
        $this->assertResponseStatus(200);
    }

    /**
     * @When I attempt to login :count times with wrong password
     */
    public function iAttemptLoginMultipleTimes(int $count): void
    {
        for ($i = 0; $i < $count; $i++) {
            $this->fillField('email', 'user@example.com');
            $this->fillField('password', 'wrong_' . $i);
            $this->pressButton('Login');
        }
    }

    /**
     * @Then I should receive a lockout notification email
     */
    public function iShouldReceiveLockoutEmail(): void
    {
        // Check mail catcher or test mail service
        $response = file_get_contents($this->baseUrl . '/api/test/emails/latest');
        $email = json_decode($response, true);
        assert(str_contains($email['subject'], 'Account Locked'));
    }

    /**
     * @Given the following users exist:
     */
    public function theFollowingUsersExist(TableNode $table): void
    {
        foreach ($table->getHash() as $row) {
            $this->createTestUser($row['name'], $row['email'], $row['role'] ?? 'user');
        }
    }

    private function createTestUser(string $name, string $email, string $role): void
    {
        $client = new \GuzzleHttp\Client();
        $response = $client->post($this->baseUrl . '/api/test/users', [
            'json' => compact('name', 'email', 'role')
        ]);
        $this->testUsers[] = json_decode($response->getBody(), true);
    }
}

API Testing Context

php
<?php
// features/bootstrap/ApiContext.php

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class ApiContext implements Context
{
    private Client $client;
    private ?object $response = null;
    private array $headers = ['Content-Type' => 'application/json'];
    private ?string $authToken = null;

    public function __construct(string $baseUrl = 'http://localhost:8000')
    {
        $this->client = new Client([
            'base_uri' => $baseUrl,
            'http_errors' => false,
        ]);
    }

    /**
     * @Given I am authenticated as :role
     */
    public function iAmAuthenticatedAs(string $role): void
    {
        $response = $this->client->post('/api/auth/login', [
            'json' => [
                'email' => "{$role}@example.com",
                'password' => 'TestPass123',
            ],
        ]);
        $data = json_decode($response->getBody(), true);
        $this->authToken = $data['token'];
        $this->headers['Authorization'] = "Bearer {$this->authToken}";
    }

    /**
     * @When I send a :method request to :url
     */
    public function iSendRequest(string $method, string $url): void
    {
        $this->response = $this->client->request($method, $url, [
            'headers' => $this->headers,
        ]);
    }

    /**
     * @When I send a :method request to :url with body:
     */
    public function iSendRequestWithBody(string $method, string $url, PyStringNode $body): void
    {
        $this->response = $this->client->request($method, $url, [
            'headers' => $this->headers,
            'body' => $body->getRaw(),
        ]);
    }

    /**
     * @Then the response status code should be :statusCode
     */
    public function theResponseStatusCodeShouldBe(int $statusCode): void
    {
        $actual = $this->response->getStatusCode();
        assert($actual === $statusCode, "Expected status {$statusCode}, got {$actual}");
    }

    /**
     * @Then the response should contain JSON key :key with value :value
     */
    public function responseContainsJsonKeyValue(string $key, string $value): void
    {
        $data = json_decode($this->response->getBody(), true);
        assert(isset($data[$key]), "Key '{$key}' not found in response");
        assert((string) $data[$key] === $value, "Expected '{$value}', got '{$data[$key]}'");
    }

    /**
     * @Then the response should contain :count items
     */
    public function responseContainsItems(int $count): void
    {
        $data = json_decode($this->response->getBody(), true);
        $actual = is_array($data) ? count($data) : count($data['data'] ?? []);
        assert($actual === $count, "Expected {$count} items, got {$actual}");
    }
}

Page Object Pattern

php
<?php
// src/Page/BasePage.php

namespace App\Page;

use Behat\Mink\Session;
use Behat\Mink\Element\NodeElement;

abstract class BasePage
{
    protected Session $session;
    protected int $timeout = 10;

    public function __construct(Session $session)
    {
        $this->session = $session;
    }

    protected function find(string $selector): ?NodeElement
    {
        return $this->session->getPage()->find('css', $selector);
    }

    protected function findAll(string $selector): array
    {
        return $this->session->getPage()->findAll('css', $selector);
    }

    protected function waitFor(string $selector, int $timeout = null): NodeElement
    {
        $timeout = $timeout ?? $this->timeout;
        $start = time();
        while (time() - $start < $timeout) {
            $element = $this->find($selector);
            if ($element && $element->isVisible()) {
                return $element;
            }
            usleep(250000); // 250ms
        }
        throw new \RuntimeException("Element '{$selector}' not found within {$timeout}s");
    }

    protected function fillField(string $selector, string $value): void
    {
        $element = $this->waitFor($selector);
        $element->setValue($value);
    }

    protected function click(string $selector): void
    {
        $element = $this->waitFor($selector);
        $element->click();
    }

    protected function getText(string $selector): string
    {
        return $this->waitFor($selector)->getText();
    }

    public function getCurrentUrl(): string
    {
        return $this->session->getCurrentUrl();
    }
}

// src/Page/LoginPage.php
namespace App\Page;

class LoginPage extends BasePage
{
    private const EMAIL_INPUT = '[data-testid="email-input"]';
    private const PASSWORD_INPUT = '[data-testid="password-input"]';
    private const SUBMIT_BUTTON = '[data-testid="login-submit"]';
    private const ERROR_MESSAGE = '[data-testid="error-message"]';

    public function open(string $baseUrl): void
    {
        $this->session->visit("{$baseUrl}/login");
    }

    public function login(string $email, string $password): void
    {
        $this->fillField(self::EMAIL_INPUT, $email);
        $this->fillField(self::PASSWORD_INPUT, $password);
        $this->click(self::SUBMIT_BUTTON);
    }

    public function getErrorMessage(): string
    {
        return $this->getText(self::ERROR_MESSAGE);
    }

    public function isLoaded(): bool
    {
        return $this->find(self::EMAIL_INPUT) !== null;
    }
}

Behat Configuration

yaml
# behat.yml
default:
  autoload:
    '': '%paths.base%/features/bootstrap'
  suites:
    ui:
      paths: ['%paths.base%/features']
      filters:
        tags: '~@api'
      contexts:
        - AuthContext:
            baseUrl: 'http://localhost:8000'
        - ShoppingContext
        - NavigationContext
    api:
      paths: ['%paths.base%/features/api']
      contexts:
        - ApiContext:
            baseUrl: 'http://localhost:8000'
  extensions:
    Behat\MinkExtension:
      base_url: 'http://localhost:8000'
      sessions:
        default:
          goutte: ~
        javascript:
          selenium2:
            browser: chrome
            capabilities:
              browserName: chrome
              extra_capabilities:
                goog:chromeOptions:
                  args:
                    - '--headless'
                    - '--no-sandbox'
                    - '--window-size=1920,1080'
  formatters:
    pretty:
      verbose: true
      paths: false

ci:
  extensions:
    Behat\MinkExtension:
      sessions:
        javascript:
          selenium2:
            browser: chrome
            capabilities:
              browserName: chrome
              extra_capabilities:
                goog:chromeOptions:
                  args:
                    - '--headless'
                    - '--no-sandbox'
                    - '--disable-dev-shm-usage'

Custom Formatter and Hooks

php
<?php
// features/bootstrap/ScreenshotContext.php

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\MinkExtension\Context\RawMinkContext;

class ScreenshotContext extends RawMinkContext implements Context
{
    private string $screenshotDir;

    public function __construct(string $screenshotDir = 'reports/screenshots')
    {
        $this->screenshotDir = $screenshotDir;
        if (!is_dir($this->screenshotDir)) {
            mkdir($this->screenshotDir, 0755, true);
        }
    }

    /**
     * @AfterStep
     */
    public function takeScreenshotOnFailure(AfterStepScope $scope): void
    {
        if ($scope->getTestResult()->getResultCode() !== 99) {
            return; // Not a failure
        }

        $driver = $this->getMink()->getSession()->getDriver();
        if (!$driver instanceof Selenium2Driver) {
            return;
        }

        $filename = sprintf(
            '%s/%s_%s_%d.png',
            $this->screenshotDir,
            date('Y-m-d_H-i-s'),
            preg_replace('/[^a-zA-Z0-9]/', '_', $scope->getStep()->getText()),
            $scope->getStep()->getLine()
        );

        file_put_contents($filename, $driver->getScreenshot());
        echo "Screenshot: {$filename}\n";
    }
}

CI/CD Integration (GitHub Actions)

yaml
name: Behat BDD Tests
on: [push, pull_request]

jobs:
  behat:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_DATABASE: test_db
          MYSQL_ROOT_PASSWORD: secret
        ports: ['3306:3306']
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          php-version: '8.3'
          extensions: pdo_mysql, mbstring
      - run: composer install --no-interaction
      - name: Start application
        run: php -S localhost:8000 -t public &
        env:
          DATABASE_URL: mysql://root:secret@127.0.0.1:3306/test_db
      - name: Run Behat tests
        run: vendor/bin/behat --profile=ci --format=junit --out=reports/junit
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: test-reports
          path: reports/

Best Practices

  1. Use Mink's built-in steps first before writing custom step definitions. Mink provides dozens of ready-to-use steps for navigation, forms, and assertions.
  2. Separate contexts by domain — AuthContext, CartContext, ApiContext. Each context should handle one area of the application.
  3. Use constructor injection in contexts for configuration values (base URL, credentials, timeouts).
  4. Tag JavaScript-dependent scenarios with @javascript to automatically use Selenium driver instead of Goutte.
  5. Use Background for shared Given steps across scenarios within a feature instead of repeating setup steps.
  6. Implement screenshot-on-failure in hooks to capture visual evidence for debugging failed scenarios.
  7. Use Scenario Outline with Examples tables for data-driven testing instead of duplicating scenarios.
  8. Configure multiple suites (UI, API, unit) in behat.yml with appropriate contexts and filters.
  9. Run database transactions in hooks — wrap each scenario in a transaction and roll back after, keeping the database clean.
  10. Use profiles for different environments (dev, CI, staging) with environment-specific configuration overrides.

Anti-Patterns to Avoid

  1. Avoid monolithic FeatureContext — Do not put all step definitions in one class. Split by domain into focused context classes.
  2. Avoid technical Gherkin — When I click CSS selector .btn-primary is wrong. Use When I submit the form for business-readable scenarios.
  3. Avoid scenario coupling — Scenarios must be independent. Never rely on a previous scenario's side effects.
  4. Avoid sleep() calls — Use Mink's waitFor() or custom wait helpers instead of sleep() for timing.
  5. Avoid hardcoded URLs — Pass base URL through behat.yml configuration or constructor parameters.
  6. Avoid mixing concerns — Step definitions should not contain SQL queries, HTTP requests, and browser interactions in the same class.
  7. Avoid missing cleanup — Always clean up test data in @AfterScenario hooks. Leftover data causes flaky subsequent tests.
  8. Avoid ignoring Goutte — Use the fast Goutte driver for non-JavaScript scenarios. Only use Selenium when JavaScript interaction is required.
  9. Avoid long scenarios — Keep scenarios under 10 steps. Long scenarios indicate the feature needs decomposition.
  10. Avoid undocumented steps — Add PHPDoc comments to all step definition methods explaining their purpose and parameters.

常见问题

Behat BDD Testing AI 技能有什么作用?

PHP BDD testing with Behat framework using Gherkin feature files, Mink browser extension, context classes, and Symfony integration for behavior-driven acceptance testing.

为什么在 TypingMind 中使用 Behat BDD Testing?

因为您只需安装一次,就能搭配任何模型使用。Behat BDD Testing 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Behat BDD Testing?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/PramodDutta/qaskills/tree/main/seed-skills/behat-bdd-testing。TypingMind 会读取其中的 SKILL.md,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Behat BDD Testing?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Behat BDD Testing 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Behat BDD Testing 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Behat BDD Testing AI 技能免费吗?

是的。PramodDutta 已在 GitHub 上根据 MIT 许可证发布了该技能。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇